mirror of
https://github.com/microsoft/autogen.git
synced 2025-11-14 17:13:29 +00:00
feat: Enable Services mapping without passing in HostApplicationBuilder
* update getting-started sample
This commit is contained in:
parent
7a6f4ded2e
commit
235a3bfab3
@ -1,13 +1,14 @@
|
|||||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||||
// Program.cs
|
// Program.cs
|
||||||
|
|
||||||
|
using Microsoft.AutoGen.Core;
|
||||||
|
using Microsoft.AutoGen.Contracts;
|
||||||
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
|
||||||
|
using Samples;
|
||||||
|
|
||||||
using TerminationF = System.Func<int, bool>;
|
using TerminationF = System.Func<int, bool>;
|
||||||
using ModifyF = System.Func<int, int>;
|
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;
|
ModifyF modifyFunc = (int x) => x - 1;
|
||||||
TerminationF runUntilFunc = (int x) =>
|
TerminationF runUntilFunc = (int x) =>
|
||||||
@ -15,14 +16,13 @@ TerminationF runUntilFunc = (int x) =>
|
|||||||
return x <= 1;
|
return x <= 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
HostApplicationBuilder builder = new HostApplicationBuilder();
|
AgentsAppBuilder appBuilder = new AgentsAppBuilder();
|
||||||
builder.Services.TryAddSingleton(modifyFunc);
|
appBuilder.Services.TryAddSingleton(modifyFunc);
|
||||||
builder.Services.TryAddSingleton(runUntilFunc);
|
appBuilder.Services.TryAddSingleton(runUntilFunc);
|
||||||
|
|
||||||
AgentsAppBuilder agentApp = new(builder);
|
appBuilder.AddAgent<Checker>("Checker");
|
||||||
agentApp.AddAgent<Checker>("Checker");
|
appBuilder.AddAgent<Modifier>("Modifier");
|
||||||
agentApp.AddAgent<Modifier>("Modifier");
|
var app = await appBuilder.BuildAsync();
|
||||||
var app = await agentApp.BuildAsync();
|
|
||||||
|
|
||||||
// Send the initial count to the agents app, running on the `local` runtime, and pass through the registered services via the application `builder`
|
// 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
|
await app.PublishMessageAsync(new CountMessage
|
||||||
@ -31,4 +31,4 @@ await app.PublishMessageAsync(new CountMessage
|
|||||||
}, new TopicId("default"));
|
}, new TopicId("default"));
|
||||||
|
|
||||||
// Run until application shutdown
|
// Run until application shutdown
|
||||||
await app.WaitForShutdownAsync();
|
await app.WaitForShutdownAsync();
|
||||||
|
|||||||
@ -21,6 +21,8 @@ public class AgentsAppBuilder
|
|||||||
this.builder.Services.AddSingleton<IAgentRuntime, InProcessRuntime>();
|
this.builder.Services.AddSingleton<IAgentRuntime, InProcessRuntime>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IServiceCollection Services => this.builder.Services;
|
||||||
|
|
||||||
public void AddAgentsFromAssemblies()
|
public void AddAgentsFromAssemblies()
|
||||||
{
|
{
|
||||||
this.AddAgentsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
|
this.AddAgentsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user