Skip to content

Commit b74150f

Browse files
authored
Merge branch 'main' into users/tracyboehrer/cards-sample
2 parents 04dcf7e + c703a86 commit b74150f

File tree

1 file changed

+86
-24
lines changed

1 file changed

+86
-24
lines changed

src/libraries/Hosting/AspNetCore/ServiceCollectionExtensions.cs

Lines changed: 86 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Microsoft.Agents.Builder.App.UserAuth;
77
using Microsoft.Agents.Builder.App;
88
using Microsoft.Agents.Hosting.AspNetCore.BackgroundQueue;
9-
using Microsoft.Agents.Storage;
109
using Microsoft.Extensions.DependencyInjection;
1110
using Microsoft.Extensions.Hosting;
1211
using System;
@@ -16,35 +15,89 @@ namespace Microsoft.Agents.Hosting.AspNetCore
1615
{
1716
public static class ServiceCollectionExtensions
1817
{
19-
public static IHostApplicationBuilder AddAgent(this IHostApplicationBuilder builder, Func<IServiceProvider, IAgent> implementationFactory, IStorage storage = null)
18+
/// <summary>
19+
/// Adds an Agent which subclasses <c>AgentApplication</c>
20+
/// <code>
21+
/// builder.Services.AddSingleton&lt;IStorage, MemoryStorage&gt;();
22+
/// builder.AddAgentApplicationOptions();
23+
/// builder.AddAgent&lt;MyAgent&gt;();
24+
/// </code>
25+
/// </summary>
26+
/// <remarks>
27+
/// This will also call <see cref="AddAgentCore(IHostApplicationBuilder)"/> and uses <c>CloudAdapter</c>.
28+
/// The Agent is registered as Transient.
29+
/// </remarks>
30+
/// <typeparam name="TAgent"></typeparam>
31+
/// <param name="builder"></param>
32+
public static IHostApplicationBuilder AddAgent<TAgent>(this IHostApplicationBuilder builder)
33+
where TAgent : class, IAgent
2034
{
21-
return AddAgent<CloudAdapter>(builder, implementationFactory, storage);
35+
return AddAgent<TAgent, CloudAdapter>(builder);
2236
}
2337

24-
public static IHostApplicationBuilder AddAgent<TAdapter>(this IHostApplicationBuilder builder, Func<IServiceProvider, IAgent> implementationFactory, IStorage storage = null)
38+
/// <summary>
39+
/// Same as <see cref="AddAgent{TAgent}(IHostApplicationBuilder)"/> but allows for use of
40+
/// any <c>CloudAdapter</c> subclass.
41+
/// </summary>
42+
/// <typeparam name="TAgent"></typeparam>
43+
/// <typeparam name="TAdapter"></typeparam>
44+
/// <param name="builder"></param>
45+
public static IHostApplicationBuilder AddAgent<TAgent, TAdapter>(this IHostApplicationBuilder builder)
46+
where TAgent : class, IAgent
2547
where TAdapter : CloudAdapter
2648
{
27-
AddCore<TAdapter>(builder, storage);
49+
AddAgentCore<TAdapter>(builder);
2850

29-
builder.Services.AddTransient<IAgent>(implementationFactory);
51+
// Add the Agent
52+
builder.Services.AddTransient<IAgent, TAgent>();
3053

3154
return builder;
3255
}
3356

34-
public static IHostApplicationBuilder AddAgent<TAgent>(this IHostApplicationBuilder builder, IStorage storage = null)
35-
where TAgent : class, IAgent
57+
/// <summary>
58+
/// Adds an Agent via lambda construction.
59+
/// <code>
60+
/// builder.Services.AddSingleton&lt;IStorage, MemoryStorage&gt;();
61+
/// builder.AddAgentApplicationOptions();
62+
/// builder.AddAgent(sp =>
63+
/// {
64+
/// var options = new AgentApplicationOptions()
65+
/// {
66+
/// TurnStateFactory = () => new TurnState(sp.GetService&lt;IStorage&gt;());
67+
/// };
68+
///
69+
/// var app = new AgentApplication(options);
70+
///
71+
/// ...
72+
///
73+
/// return app;
74+
/// });
75+
/// </code>
76+
/// </summary>
77+
/// <remarks>
78+
/// This will also calls <see cref="AddAgentCore(IHostApplicationBuilder)"/> and uses <c>CloudAdapter</c>.
79+
/// The Agent is registered as Transient.
80+
/// </remarks>
81+
/// <param name="builder"></param>
82+
/// <param name="implementationFactory"></param>
83+
public static IHostApplicationBuilder AddAgent(this IHostApplicationBuilder builder, Func<IServiceProvider, IAgent> implementationFactory)
3684
{
37-
return AddAgent<TAgent, CloudAdapter>(builder, storage);
85+
return AddAgent<CloudAdapter>(builder, implementationFactory);
3886
}
3987

40-
public static IHostApplicationBuilder AddAgent<TAgent, TAdapter>(this IHostApplicationBuilder builder, IStorage storage = null)
41-
where TAgent : class, IAgent
88+
/// <summary>
89+
/// This is the same as <see cref="AddAgent(IHostApplicationBuilder, Func{IServiceProvider, IAgent})"/>, except allows the
90+
/// use of any <c>CloudAdapter</c> subclass.
91+
/// </summary>
92+
/// <typeparam name="TAdapter"></typeparam>
93+
/// <param name="builder"></param>
94+
/// <param name="implementationFactory"></param>
95+
public static IHostApplicationBuilder AddAgent<TAdapter>(this IHostApplicationBuilder builder, Func<IServiceProvider, IAgent> implementationFactory)
4296
where TAdapter : CloudAdapter
4397
{
44-
AddCore<TAdapter>(builder, storage);
98+
AddAgentCore<TAdapter>(builder);
4599

46-
// Add the Agent
47-
builder.Services.AddTransient<IAgent, TAgent>();
100+
builder.Services.AddTransient<IAgent>(implementationFactory);
48101

49102
return builder;
50103
}
@@ -53,7 +106,8 @@ public static IHostApplicationBuilder AddAgent<TAgent, TAdapter>(this IHostAppli
53106
/// Registers AgentApplicationOptions for AgentApplication-based Agents.
54107
/// </summary>
55108
/// <remarks>
56-
/// This loads options from IConfiguration and DI.
109+
/// This loads options from IConfiguration and DI. The <c>AgentApplicationOptions</c> is
110+
/// added as a singleton.
57111
/// </remarks>
58112
/// <param name="builder"></param>
59113
/// <param name="fileDownloaders"></param>
@@ -90,7 +144,7 @@ public static void AddCloudAdapter(this IServiceCollection services)
90144
}
91145

92146
/// <summary>
93-
/// Add the derived CloudAdapter.
147+
/// Add a derived CloudAdapter.
94148
/// </summary>
95149
/// <param name="services"></param>
96150
/// <param name="async"></param>
@@ -103,9 +157,21 @@ public static void AddCloudAdapter<T>(this IServiceCollection services) where T
103157
services.AddSingleton<IChannelAdapter>(sp => sp.GetService<CloudAdapter>());
104158
}
105159

160+
/// <summary>
161+
/// Adds the core agent services.
162+
/// <list type="bullet">
163+
/// <item><c>IConnections, which uses IConfiguration for settings.</c></item>
164+
/// <item><c>IChannelServiceClientFactory</c> for ConnectorClient and UserTokenClient creations. Needed for Azure Bot Service and Agent-to-Agent.</item>
165+
/// <item><c>CloudAdapter</c>, this is the default adapter that works with Azure Bot Service and Activity Protocol Agents.</item>
166+
/// </list>
167+
/// </summary>
168+
/// <param name="builder"></param>
169+
public static IHostApplicationBuilder AddAgentCore(this IHostApplicationBuilder builder)
170+
{
171+
return builder.AddAgentCore<CloudAdapter>();
172+
}
106173

107-
108-
private static void AddCore<TAdapter>(this IHostApplicationBuilder builder, IStorage storage = null)
174+
public static IHostApplicationBuilder AddAgentCore<TAdapter>(this IHostApplicationBuilder builder)
109175
where TAdapter : CloudAdapter
110176
{
111177
// Add Connections object to access configured token connections.
@@ -114,14 +180,10 @@ private static void AddCore<TAdapter>(this IHostApplicationBuilder builder, ISto
114180
// Add factory for ConnectorClient and UserTokenClient creation
115181
builder.Services.AddSingleton<IChannelServiceClientFactory, RestChannelServiceClientFactory>();
116182

117-
// Add IStorage for turn state persistence
118-
if (storage != null)
119-
{
120-
builder.Services.AddSingleton(storage);
121-
}
122-
123183
// Add the CloudAdapter, this is the default adapter that works with Azure Bot Service and Activity Protocol Agents.
124184
AddCloudAdapter<TAdapter>(builder.Services);
185+
186+
return builder;
125187
}
126188

127189
private static void AddAsyncCloudAdapterSupport(this IServiceCollection services)

0 commit comments

Comments
 (0)