-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathIServiceCollectionExtensions.cs
316 lines (273 loc) · 12.7 KB
/
IServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System.ComponentModel;
using JustSaying;
using JustSaying.AwsTools;
using JustSaying.AwsTools.QueueCreation;
using JustSaying.Fluent;
using JustSaying.Messaging;
using JustSaying.Messaging.Channels.Receive;
using JustSaying.Messaging.Compression;
using JustSaying.Messaging.MessageHandling;
using JustSaying.Messaging.MessageSerialization;
using JustSaying.Messaging.Middleware.Logging;
using JustSaying.Messaging.Middleware.PostProcessing;
using JustSaying.Messaging.Monitoring;
using JustSaying.Models;
using JustSaying.Naming;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// A class containing extension methods for the <see cref="IServiceCollection"/> interface. This class cannot be inherited.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IServiceCollectionExtensions
{
/// <summary>
/// Adds JustSaying services to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add JustSaying services to.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection AddJustSaying(this IServiceCollection services)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
return services.AddJustSaying((_) => { });
}
/// <summary>
/// Adds JustSaying services to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add JustSaying services to.</param>
/// <param name="region">The AWS region to configure.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> or <paramref name="region"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection AddJustSaying(this IServiceCollection services, string region)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (string.IsNullOrWhiteSpace(region))
{
throw new ArgumentException("region must not be null or empty" ,nameof(region));
}
return services.AddJustSaying(
(builder) => builder.Messaging(
(options) => options.WithRegion(region)));
}
/// <summary>
/// Adds JustSaying services to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add JustSaying services to.</param>
/// <param name="configure">A delegate to a method to use to configure JustSaying.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection AddJustSaying(this IServiceCollection services, Action<MessagingBusBuilder> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
return services.AddJustSaying((builder, _) => configure(builder));
}
/// <summary>
/// Adds JustSaying services to the service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add JustSaying services to.</param>
/// <param name="configure">A delegate to a method to use to configure JustSaying.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection AddJustSaying(this IServiceCollection services, Action<MessagingBusBuilder, IServiceProvider> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
// Register as self so the same singleton instance implements two different interfaces
services.TryAddSingleton((p) => new ServiceProviderResolver(p));
services.TryAddSingleton<IHandlerResolver>((p) => p.GetRequiredService<ServiceProviderResolver>());
services.TryAddSingleton<IServiceResolver>((p) => p.GetRequiredService<ServiceProviderResolver>());
services.TryAddSingleton<IAwsClientFactory, DefaultAwsClientFactory>();
services.TryAddSingleton<IAwsClientFactoryProxy>((p) => new AwsClientFactoryProxy(p.GetRequiredService<IAwsClientFactory>));
services.TryAddSingleton<MessagingConfig>();
services.TryAddSingleton<IMessagingConfig>((p) => p.GetRequiredService<MessagingConfig>());
services.TryAddSingleton<IPublishConfiguration>((p) => p.GetRequiredService<MessagingConfig>());
services.TryAddSingleton<IPublishBatchConfiguration>((p) => p.GetRequiredService<MessagingConfig>());
services.TryAddSingleton<IMessageMonitor, NullOpMessageMonitor>();
services.TryAddTransient<LoggingMiddleware>();
services.TryAddTransient<SqsPostProcessorMiddleware>();
services.AddSingleton<MessageContextAccessor>();
services.TryAddSingleton<IMessageContextAccessor>(serviceProvider => serviceProvider.GetRequiredService<MessageContextAccessor>());
services.TryAddSingleton<IMessageContextReader>(serviceProvider => serviceProvider.GetRequiredService<MessageContextAccessor>());
services.TryAddSingleton<IMessageBodySerializationFactory, NewtonsoftSerializationFactory>();
services.TryAddSingleton<IMessageSubjectProvider, GenericMessageSubjectProvider>();
services.TryAddSingleton<IVerifyAmazonQueues, AmazonQueueCreator>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<IMessageBodyCompression, GzipMessageBodyCompression>());
services.TryAddSingleton<MessageCompressionRegistry>();
services.TryAddSingleton<IMessageReceivePauseSignal, MessageReceivePauseSignal>();
services.TryAddSingleton(
(serviceProvider) =>
{
var builder = new MessagingBusBuilder()
.WithServiceResolver(new ServiceProviderResolver(serviceProvider));
configure(builder, serviceProvider);
var contributors = serviceProvider.GetServices<IMessageBusConfigurationContributor>();
foreach (var contributor in contributors)
{
contributor.Configure(builder);
}
return builder;
});
services.TryAddSingleton(
(serviceProvider) =>
{
var builder = serviceProvider.GetRequiredService<MessagingBusBuilder>();
return builder.BuildPublisher();
});
services.TryAddSingleton(
(serviceProvider) =>
{
var publisher = serviceProvider.GetRequiredService<IMessagePublisher>();
if (publisher is IMessageBatchPublisher batchPublisher)
{
return batchPublisher;
}
var builder = serviceProvider.GetRequiredService<MessagingBusBuilder>();
return builder.BuildBatchPublisher();
});
services.TryAddSingleton(
(serviceProvider) =>
{
var builder = serviceProvider.GetRequiredService<MessagingBusBuilder>();
return builder.BuildSubscribers();
});
services.AddSingleton<DefaultNamingConventions>();
services.TryAddSingleton<ITopicNamingConvention>((s) => s.GetRequiredService<DefaultNamingConventions>());
services.TryAddSingleton<IQueueNamingConvention>((s) => s.GetRequiredService<DefaultNamingConventions>());
return services;
}
/// <summary>
/// Adds a JustSaying message handler to the service collection.
/// </summary>
/// <typeparam name="TMessage">The type of the message handled.</typeparam>
/// <typeparam name="THandler">The type of the message handler to register.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the message handler to.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection AddJustSayingHandler<TMessage, THandler>(this IServiceCollection services)
where TMessage : Message
where THandler : class, IHandlerAsync<TMessage>
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
services.TryAddTransient<IHandlerAsync<TMessage>, THandler>();
return services;
}
/// <summary>
/// Adds a JustSaying message handler to the service collection that uses the specified handlers.
/// </summary>
/// <typeparam name="TMessage">The type of the message handled.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to add the message handler to.</param>
/// <param name="handlers">The message handlers to use.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> or <paramref name="handlers"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="handlers"/> contains no handlers.
/// </exception>
public static IServiceCollection AddJustSayingHandlers<TMessage>(
this IServiceCollection services,
IEnumerable<IHandlerAsync<TMessage>> handlers)
where TMessage : Message
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (handlers == null)
{
throw new ArgumentNullException(nameof(handlers));
}
var enumeratedHandlers = handlers.ToList();
if (enumeratedHandlers.Count < 1)
{
throw new ArgumentException("At least one message handler must be specified.", nameof(handlers));
}
if (enumeratedHandlers.Count == 1)
{
services.TryAddTransient((_) => enumeratedHandlers[0]);
}
else
{
services.TryAddTransient<IHandlerAsync<TMessage>>((_) => new ListHandler<TMessage>(enumeratedHandlers));
}
return services;
}
/// <summary>
/// Configures JustSaying using the specified service collection.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to configure JustSaying with.</param>
/// <param name="configure">A delegate to a method to use to configure JustSaying.</param>
/// <returns>
/// The <see cref="IServiceCollection"/> specified by <paramref name="services"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>.
/// </exception>
public static IServiceCollection ConfigureJustSaying(this IServiceCollection services, Action<MessagingBusBuilder> configure)
{
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
return services.AddSingleton<IMessageBusConfigurationContributor>(new DelegatingConfigurationContributor(configure));
}
private sealed class DelegatingConfigurationContributor : IMessageBusConfigurationContributor
{
private readonly Action<MessagingBusBuilder> _configure;
internal DelegatingConfigurationContributor(Action<MessagingBusBuilder> configure)
{
_configure = configure;
}
public void Configure(MessagingBusBuilder builder)
=> _configure(builder);
}
}