feat: Enable Services mapping without passing in HostApplicationBuilder

* update getting-started sample
This commit is contained in:
Jacob Alber 2025-01-27 18:22:42 -05:00 committed by Jack Gerrits
parent 7a6f4ded2e
commit 235a3bfab3
2 changed files with 16 additions and 14 deletions

View File

@ -1,13 +1,14 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs
using Microsoft.AutoGen.Core;
using Microsoft.AutoGen.Contracts;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Samples;
using TerminationF = System.Func<int, bool>;
using ModifyF = System.Func<int, int>;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.Hosting;
using Samples;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AutoGen.Contracts;
ModifyF modifyFunc = (int x) => x - 1;
TerminationF runUntilFunc = (int x) =>
@ -15,14 +16,13 @@ TerminationF runUntilFunc = (int x) =>
return x <= 1;
};
HostApplicationBuilder builder = new HostApplicationBuilder();
builder.Services.TryAddSingleton(modifyFunc);
builder.Services.TryAddSingleton(runUntilFunc);
AgentsAppBuilder appBuilder = new AgentsAppBuilder();
appBuilder.Services.TryAddSingleton(modifyFunc);
appBuilder.Services.TryAddSingleton(runUntilFunc);
AgentsAppBuilder agentApp = new(builder);
agentApp.AddAgent<Checker>("Checker");
agentApp.AddAgent<Modifier>("Modifier");
var app = await agentApp.BuildAsync();
appBuilder.AddAgent<Checker>("Checker");
appBuilder.AddAgent<Modifier>("Modifier");
var app = await appBuilder.BuildAsync();
// Send the initial count to the agents app, running on the `local` runtime, and pass through the registered services via the application `builder`
await app.PublishMessageAsync(new CountMessage
@ -31,4 +31,4 @@ await app.PublishMessageAsync(new CountMessage
}, new TopicId("default"));
// Run until application shutdown
await app.WaitForShutdownAsync();
await app.WaitForShutdownAsync();

View File

@ -21,6 +21,8 @@ public class AgentsAppBuilder
this.builder.Services.AddSingleton<IAgentRuntime, InProcessRuntime>();
}
public IServiceCollection Services => this.builder.Services;
public void AddAgentsFromAssemblies()
{
this.AddAgentsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());