6
6
using Microsoft . Agents . Builder . App . UserAuth ;
7
7
using Microsoft . Agents . Builder . App ;
8
8
using Microsoft . Agents . Hosting . AspNetCore . BackgroundQueue ;
9
- using Microsoft . Agents . Storage ;
10
9
using Microsoft . Extensions . DependencyInjection ;
11
10
using Microsoft . Extensions . Hosting ;
12
11
using System ;
@@ -16,35 +15,89 @@ namespace Microsoft.Agents.Hosting.AspNetCore
16
15
{
17
16
public static class ServiceCollectionExtensions
18
17
{
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<IStorage, MemoryStorage>();
22
+ /// builder.AddAgentApplicationOptions();
23
+ /// builder.AddAgent<MyAgent>();
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
20
34
{
21
- return AddAgent < CloudAdapter > ( builder , implementationFactory , storage ) ;
35
+ return AddAgent < TAgent , CloudAdapter > ( builder ) ;
22
36
}
23
37
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
25
47
where TAdapter : CloudAdapter
26
48
{
27
- AddCore < TAdapter > ( builder , storage ) ;
49
+ AddAgentCore < TAdapter > ( builder ) ;
28
50
29
- builder . Services . AddTransient < IAgent > ( implementationFactory ) ;
51
+ // Add the Agent
52
+ builder . Services . AddTransient < IAgent , TAgent > ( ) ;
30
53
31
54
return builder ;
32
55
}
33
56
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<IStorage, MemoryStorage>();
61
+ /// builder.AddAgentApplicationOptions();
62
+ /// builder.AddAgent(sp =>
63
+ /// {
64
+ /// var options = new AgentApplicationOptions()
65
+ /// {
66
+ /// TurnStateFactory = () => new TurnState(sp.GetService<IStorage>());
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 )
36
84
{
37
- return AddAgent < TAgent , CloudAdapter > ( builder , storage ) ;
85
+ return AddAgent < CloudAdapter > ( builder , implementationFactory ) ;
38
86
}
39
87
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 )
42
96
where TAdapter : CloudAdapter
43
97
{
44
- AddCore < TAdapter > ( builder , storage ) ;
98
+ AddAgentCore < TAdapter > ( builder ) ;
45
99
46
- // Add the Agent
47
- builder . Services . AddTransient < IAgent , TAgent > ( ) ;
100
+ builder . Services . AddTransient < IAgent > ( implementationFactory ) ;
48
101
49
102
return builder ;
50
103
}
@@ -53,7 +106,8 @@ public static IHostApplicationBuilder AddAgent<TAgent, TAdapter>(this IHostAppli
53
106
/// Registers AgentApplicationOptions for AgentApplication-based Agents.
54
107
/// </summary>
55
108
/// <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.
57
111
/// </remarks>
58
112
/// <param name="builder"></param>
59
113
/// <param name="fileDownloaders"></param>
@@ -90,7 +144,7 @@ public static void AddCloudAdapter(this IServiceCollection services)
90
144
}
91
145
92
146
/// <summary>
93
- /// Add the derived CloudAdapter.
147
+ /// Add a derived CloudAdapter.
94
148
/// </summary>
95
149
/// <param name="services"></param>
96
150
/// <param name="async"></param>
@@ -103,9 +157,21 @@ public static void AddCloudAdapter<T>(this IServiceCollection services) where T
103
157
services . AddSingleton < IChannelAdapter > ( sp => sp . GetService < CloudAdapter > ( ) ) ;
104
158
}
105
159
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
+ }
106
173
107
-
108
- private static void AddCore < TAdapter > ( this IHostApplicationBuilder builder , IStorage storage = null )
174
+ public static IHostApplicationBuilder AddAgentCore < TAdapter > ( this IHostApplicationBuilder builder )
109
175
where TAdapter : CloudAdapter
110
176
{
111
177
// Add Connections object to access configured token connections.
@@ -114,14 +180,10 @@ private static void AddCore<TAdapter>(this IHostApplicationBuilder builder, ISto
114
180
// Add factory for ConnectorClient and UserTokenClient creation
115
181
builder . Services . AddSingleton < IChannelServiceClientFactory , RestChannelServiceClientFactory > ( ) ;
116
182
117
- // Add IStorage for turn state persistence
118
- if ( storage != null )
119
- {
120
- builder . Services . AddSingleton ( storage ) ;
121
- }
122
-
123
183
// Add the CloudAdapter, this is the default adapter that works with Azure Bot Service and Activity Protocol Agents.
124
184
AddCloudAdapter < TAdapter > ( builder . Services ) ;
185
+
186
+ return builder ;
125
187
}
126
188
127
189
private static void AddAsyncCloudAdapterSupport ( this IServiceCollection services )
0 commit comments