-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCoreExServiceBusExtensions.DependencyInjection.cs
More file actions
425 lines (386 loc) · 27.1 KB
/
Copy pathCoreExServiceBusExtensions.DependencyInjection.cs
File metadata and controls
425 lines (386 loc) · 27.1 KB
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#pragma warning disable IDE0130 // Namespace does not match folder structure; by design.
namespace Microsoft.Extensions.DependencyInjection;
#pragma warning restore IDE0130 // Namespace does not match folder structure
/// <summary>
/// Provides standard extensions.
/// </summary>
public static class CoreExServiceBusExtensions
{
/// <summary>
/// Adds a keyed <b>scoped</b> Azure <see cref="ServiceBusPublisher"/> service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusPublisher"/> instance.</param>
/// <param name="addAsDefaultIEventPublisher">Indicates whether to also register as the default (non-keyed) <see cref="IEventPublisher"/> service.</param>
/// <param name="serviceKey">The service key to use for the keyed registration.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
/// <remarks>See <see cref="Microsoft.Extensions.DependencyInjection.CoreExEventsExtensions.AddEventPublisher(IServiceCollection, string, Func{IServiceProvider, IEventPublisher}, bool)"/> for more information
/// related to the underlying registration implementation.</remarks>
public static IServiceCollection AddAzureServiceBusPublisher(this IServiceCollection services, Action<IServiceProvider, ServiceBusPublisher>? configure = null, bool addAsDefaultIEventPublisher = true, string serviceKey = ServiceBusPublisher.DefaultServiceKey)
=> services.ThrowIfNull().AddEventPublisher(serviceKey, sp =>
{
var sbp = ActivatorUtilities.CreateInstance<ServiceBusPublisher>(sp);
configure?.Invoke(sp, sbp);
return sbp;
}, addAsDefaultIEventPublisher);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusSubscribedSubscriber"/> service.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusSubscribedSubscriber"/> instance.</param>
/// <returns>The <see cref="IServiceCollection"/> for fluent-style method-chaining.</returns>
public static IServiceCollection AddAzureServiceBusSubscribedSubscriber(this IServiceCollection services, Action<IServiceProvider, ServiceBusSubscribedSubscriber>? configure = null)
{
return services.ThrowIfNull().AddSingleton(sp =>
{
var sbss = ActivatorUtilities.CreateInstance<ServiceBusSubscribedSubscriber>(sp);
configure?.Invoke(sp, sbss);
return sbss;
});
}
/// <summary>
/// Provides a builder to register the Azure Service Bus receiving services.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="AzureServiceBusReceiveServiceBuilder"/>.</returns>
/// <remarks>Provides a fluent-style builder for configuring and registering the related Azure Service Bus receiver services to simplify usage and minimize challenges with the configuration hierarchy.</remarks>
public static AzureServiceBusReceiveServiceBuilder AzureServiceBusReceiving(this IServiceCollection services) => new(services);
/// <summary>
/// Provides a builder for configuring and registering Azure Service Bus receiver services .
/// </summary>
public sealed class AzureServiceBusReceiveServiceBuilder
{
private readonly IServiceCollection _services;
/// <summary>
/// Initializes a new instance of the <see cref="AzureServiceBusReceiveServiceBuilder"/> class.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
internal AzureServiceBusReceiveServiceBuilder(IServiceCollection services) => _services = services.ThrowIfNull();
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusReceiver"/> service enabling ongoing fluent-style method-chaining registration.
/// </summary>
/// <param name="optionsFactory">The factory to create the required <see cref="CoreEx.Azure.Messaging.ServiceBus.ServiceBusReceiverOptions"/>.</param>
/// <returns>The <see cref="AzureServiceBusReceiverService"/> for fluent-style method-chaining.</returns>
public AzureServiceBusReceiverService WithReceiver(Func<IServiceProvider, CoreEx.Azure.Messaging.ServiceBus.ServiceBusReceiverOptions> optionsFactory) => new(_services, null, optionsFactory);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusSessionReceiver"/> service enabling ongoing fluent-style method-chaining registration.
/// </summary>
/// <param name="optionsFactory">The factory to create the required <see cref="CoreEx.Azure.Messaging.ServiceBus.ServiceBusReceiverOptions"/>.</param>
/// <returns>The <see cref="AzureServiceBusSessionReceiverService"/> for fluent-style method-chaining.</returns>
public AzureServiceBusSessionReceiverService WithSessionReceiver(Func<IServiceProvider, CoreEx.Azure.Messaging.ServiceBus.ServiceBusSessionReceiverOptions> optionsFactory) => new(_services, null, optionsFactory);
/// <summary>
/// Provides the <see cref="ServiceBusReceiver{TSubscriber}"/> service registration.
/// </summary>
public sealed class AzureServiceBusReceiverService
{
/// <summary>
/// Initializes a new instance of the <see cref="AzureServiceBusReceiverService"/> class.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="serviceKey">The service key.</param>
/// <param name="optionsFactory">The factory to create the required <see cref="CoreEx.Azure.Messaging.ServiceBus.ServiceBusReceiverOptions"/>.</param>
internal AzureServiceBusReceiverService(IServiceCollection services, object? serviceKey, Func<IServiceProvider, CoreEx.Azure.Messaging.ServiceBus.ServiceBusReceiverOptions> optionsFactory)
{
Services = services.ThrowIfNull();
ServiceKey = serviceKey;
OptionsFactory = optionsFactory.ThrowIfNull();
}
/// <summary>
/// Gets the <see cref="IServiceCollection"/>.
/// </summary>
internal IServiceCollection Services { get; }
/// <summary>
/// Gets the service key.
/// </summary>
internal object? ServiceKey { get; }
/// <summary>
/// Gets the options factory.
/// </summary>
internal Func<IServiceProvider, CoreEx.Azure.Messaging.ServiceBus.ServiceBusReceiverOptions> OptionsFactory { get; }
/// <summary>
/// Adds a <b>singleton</b> Azure Service Bus <typeparamref name="TSubscriber"/> (see <see cref="CoreEx.Azure.Messaging.ServiceBus.Abstractions.ServiceBusReceiverBase"/>).
/// </summary>
/// <typeparam name="TSubscriber">The Azure <see cref="ServiceBusSubscriberBase"/> <see cref="Type"/>.</typeparam>
/// <param name="configure">An optional action to configure the <typeparamref name="TSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSubscriberService<TSubscriber> WithSubscriber<TSubscriber>(Action<IServiceProvider, TSubscriber>? configure = null) where TSubscriber : ServiceBusSubscriberBase
=> new(this, null, configure);
/// <summary>
/// Adds a <b>singleton</b> Azure Service Bus <typeparamref name="TSubscriber"/> (see <see cref="CoreEx.Azure.Messaging.ServiceBus.Abstractions.ServiceBusReceiverBase"/>).
/// </summary>
/// <typeparam name="TSubscriber">The Azure <see cref="ServiceBusSubscriberBase"/> <see cref="Type"/>.</typeparam>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <typeparamref name="TSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSubscriberService<TSubscriber> WithKeyedSubscriber<TSubscriber>(object serviceKey, Action<IServiceProvider, TSubscriber>? configure = null) where TSubscriber : ServiceBusSubscriberBase
=> new(this, serviceKey, configure);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusSubscribedSubscriber"/> as the subscriber.
/// </summary>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusSubscribedSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSubscriberService<ServiceBusSubscribedSubscriber> WithSubscribedSubscriber(Action<IServiceProvider, ServiceBusSubscribedSubscriber>? configure = null) => WithSubscriber(configure);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusSubscribedSubscriber"/> as the subscriber.
/// </summary>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusSubscribedSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSubscriberService<ServiceBusSubscribedSubscriber> WithKeyedSubscribedSubscriber(object serviceKey, Action<IServiceProvider, ServiceBusSubscribedSubscriber>? configure = null) => WithKeyedSubscriber(serviceKey, configure);
}
/// <summary>
/// Provides the <typeparamref name="TSubscriber"/> service registration.
/// </summary>
/// <typeparam name="TSubscriber"></typeparam>
public sealed class WithSubscriberService<TSubscriber> where TSubscriber : ServiceBusSubscriberBase
{
private readonly AzureServiceBusReceiverService _owner;
private readonly object? _serviceKey;
private readonly Action<IServiceProvider, TSubscriber>? _configure;
/// <summary>
/// Initializes a new instance of the <see cref="WithSubscriberService{TSubscriber}"/> class.
/// </summary>
/// <param name="owner">The owner <see cref="AzureServiceBusReceiverService"/> instance.</param>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <typeparamref name="TSubscriber"/> instance.</param>
internal WithSubscriberService(AzureServiceBusReceiverService owner, object? serviceKey, Action<IServiceProvider, TSubscriber>? configure)
{
_owner = owner.ThrowIfNull();
_serviceKey = serviceKey;
_configure = configure;
}
/// <summary>
/// Builds and registers all of the chained services.
/// </summary>
/// <remarks>Where a hosted service is also required then the chained <see cref="WithHostedService"/> should be used instead.</remarks>
public void Build()
{
// Add the subscriber service.
if (_serviceKey is null)
_owner.Services.AddSingleton(sp =>
{
var subscriber = ActivatorUtilities.CreateInstance<TSubscriber>(sp);
_configure?.Invoke(sp, subscriber);
return subscriber;
});
else
_owner.Services.AddKeyedSingleton(_serviceKey, (sp, _) =>
{
var subscriber = ActivatorUtilities.CreateInstance<TSubscriber>(sp);
_configure?.Invoke(sp, subscriber);
return subscriber;
});
// Add the receiver service.
if (_owner.ServiceKey is null)
_owner.Services.AddSingleton(sp =>
{
var options = _owner.OptionsFactory(sp) ?? throw new InvalidOperationException("The options factory must return a non-null.");
options.SubscriberServiceKey = _serviceKey;
var receiver = ActivatorUtilities.CreateInstance<ServiceBusReceiver<TSubscriber>>(sp, options);
return receiver;
});
else
_owner.Services.AddKeyedSingleton(_owner.ServiceKey, (sp, _) =>
{
var options = _owner.OptionsFactory(sp) ?? throw new InvalidOperationException("The options factory must return a non-null.");
options.SubscriberServiceKey = _serviceKey;
var receiver = ActivatorUtilities.CreateInstance<ServiceBusReceiver<TSubscriber>>(sp, options);
return receiver;
});
}
/// <summary>
/// Create the receiver instance.
/// </summary>
private ServiceBusReceiver<TSubscriber> GetReceiverInstance(IServiceProvider serviceProvider) => _serviceKey is null
? serviceProvider.GetRequiredService<ServiceBusReceiver<TSubscriber>>()
: serviceProvider.GetRequiredKeyedService<ServiceBusReceiver<TSubscriber>>(_serviceKey);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusReceiverHostedService{TReceiver}"/> keyed service that will be executed as a hosted service (i.e. in the background).
/// </summary>
/// <param name="serviceKey">The keyed singleton and health check key.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusReceiverHostedService{TReceiver}"/> instance.</param>
/// <returns>The <see cref="WithHostedServiceService{TReceiver}"/> instance for fluent-style method-chaining.</returns>
/// <remarks>No services are added until the chained <see cref="WithHostedServiceService{TReceiver}.Build"/> method is called.</remarks>
public WithHostedServiceService<ServiceBusReceiver<TSubscriber>> WithHostedService(string serviceKey = "azure-service-bus-receiver", Action<IServiceProvider, ServiceBusReceiverHostedService<ServiceBusReceiver<TSubscriber>>>? configure = null)
=> new(_owner.Services, serviceKey.ThrowIfNullOrEmpty(), configure, Build, GetReceiverInstance);
}
/// <summary>
/// Provides the <see cref="ServiceBusSessionReceiver{TSubscriber}"/> service registration.
/// </summary>
public sealed class AzureServiceBusSessionReceiverService
{
/// <summary>
/// Initializes a new instance of the <see cref="AzureServiceBusSessionReceiverService"/> class.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="serviceKey">The service key.</param>
/// <param name="optionsFactory">The factory to create the required <see cref="CoreEx.Azure.Messaging.ServiceBus.ServiceBusSessionReceiverOptions"/>.</param>
internal AzureServiceBusSessionReceiverService(IServiceCollection services, object? serviceKey, Func<IServiceProvider, CoreEx.Azure.Messaging.ServiceBus.ServiceBusSessionReceiverOptions> optionsFactory)
{
Services = services.ThrowIfNull();
ServiceKey = serviceKey;
OptionsFactory = optionsFactory.ThrowIfNull();
}
/// <summary>
/// Gets the <see cref="IServiceCollection"/>.
/// </summary>
internal IServiceCollection Services { get; }
/// <summary>
/// Gets the service key.
/// </summary>
internal object? ServiceKey { get; }
/// <summary>
/// Gets the options factory.
/// </summary>
internal Func<IServiceProvider, CoreEx.Azure.Messaging.ServiceBus.ServiceBusSessionReceiverOptions> OptionsFactory { get; }
/// <summary>
/// Adds a <b>singleton</b> Azure Service Bus <typeparamref name="TSubscriber"/> (see <see cref="CoreEx.Azure.Messaging.ServiceBus.Abstractions.ServiceBusReceiverBase"/>).
/// </summary>
/// <typeparam name="TSubscriber">The Azure <see cref="ServiceBusSubscriberBase"/> <see cref="Type"/>.</typeparam>
/// <param name="configure">An optional action to configure the <typeparamref name="TSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSessionSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSessionSubscriberService<TSubscriber> WithSubscriber<TSubscriber>(Action<IServiceProvider, TSubscriber>? configure = null) where TSubscriber : ServiceBusSubscriberBase
=> new(this, null, configure);
/// <summary>
/// Adds a <b>singleton</b> Azure Service Bus <typeparamref name="TSubscriber"/> (see <see cref="CoreEx.Azure.Messaging.ServiceBus.Abstractions.ServiceBusReceiverBase"/>).
/// </summary>
/// <typeparam name="TSubscriber">The Azure <see cref="ServiceBusSubscriberBase"/> <see cref="Type"/>.</typeparam>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <typeparamref name="TSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSessionSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSessionSubscriberService<TSubscriber> WithKeyedSubscriber<TSubscriber>(object serviceKey, Action<IServiceProvider, TSubscriber>? configure = null) where TSubscriber : ServiceBusSubscriberBase
=> new(this, serviceKey, configure);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusSubscribedSubscriber"/> as the subscriber.
/// </summary>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusSubscribedSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSessionSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSessionSubscriberService<ServiceBusSubscribedSubscriber> WithSubscribedSubscriber(Action<IServiceProvider, ServiceBusSubscribedSubscriber>? configure = null) => WithSubscriber(configure);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusSubscribedSubscriber"/> as the subscriber.
/// </summary>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusSubscribedSubscriber"/> instance.</param>
/// <returns>The <see cref="WithSessionSubscriberService{TSubscriber}"/> for fluent-style method-chaining.</returns>
public WithSessionSubscriberService<ServiceBusSubscribedSubscriber> WithKeyedSubscribedSubscriber(object serviceKey, Action<IServiceProvider, ServiceBusSubscribedSubscriber>? configure = null) => WithKeyedSubscriber(serviceKey, configure);
}
/// <summary>
/// Provides the <typeparamref name="TSubscriber"/> service registration.
/// </summary>
/// <typeparam name="TSubscriber">The <see cref="ServiceBusSubscriberBase"/> <see cref="Type"/>.</typeparam>
public sealed class WithSessionSubscriberService<TSubscriber> where TSubscriber : ServiceBusSubscriberBase
{
private readonly AzureServiceBusSessionReceiverService _owner;
private readonly object? _serviceKey;
private readonly Action<IServiceProvider, TSubscriber>? _configure;
/// <summary>
/// Initializes a new instance of the <see cref="WithSubscriberService{TSubscriber}"/> class.
/// </summary>
/// <param name="owner">The owner <see cref="AzureServiceBusSessionReceiverService"/> instance.</param>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <typeparamref name="TSubscriber"/> instance.</param>
internal WithSessionSubscriberService(AzureServiceBusSessionReceiverService owner, object? serviceKey, Action<IServiceProvider, TSubscriber>? configure)
{
_owner = owner.ThrowIfNull();
_serviceKey = serviceKey;
_configure = configure;
}
/// <summary>
/// Builds and registers all of the chained services.
/// </summary>
/// <remarks>Where a hosted service is also required then the chained <see cref="WithHostedService"/> should be used instead.</remarks>
public void Build()
{
// Add the subscriber service.
if (_serviceKey is null)
_owner.Services.AddSingleton(sp =>
{
var subscriber = ActivatorUtilities.CreateInstance<TSubscriber>(sp);
_configure?.Invoke(sp, subscriber);
return subscriber;
});
else
_owner.Services.AddKeyedSingleton(_serviceKey, (sp, _) =>
{
var subscriber = ActivatorUtilities.CreateInstance<TSubscriber>(sp);
_configure?.Invoke(sp, subscriber);
return subscriber;
});
// Add the receiver service.
if (_owner.ServiceKey is null)
_owner.Services.AddSingleton(sp =>
{
var options = _owner.OptionsFactory(sp) ?? throw new InvalidOperationException("The options factory must return a non-null.");
options.SubscriberServiceKey = _serviceKey;
var receiver = ActivatorUtilities.CreateInstance<ServiceBusSessionReceiver<TSubscriber>>(sp, options);
return receiver;
});
else
_owner.Services.AddKeyedSingleton(_owner.ServiceKey, (sp, _) =>
{
var options = _owner.OptionsFactory(sp) ?? throw new InvalidOperationException("The options factory must return a non-null.");
options.SubscriberServiceKey = _serviceKey;
var receiver = ActivatorUtilities.CreateInstance<ServiceBusSessionReceiver<TSubscriber>>(sp, options);
return receiver;
});
}
/// <summary>
/// Create the receiver instance.
/// </summary>
private ServiceBusSessionReceiver<TSubscriber> GetReceiverInstance(IServiceProvider serviceProvider) => _serviceKey is null
? serviceProvider.GetRequiredService<ServiceBusSessionReceiver<TSubscriber>>()
: serviceProvider.GetRequiredKeyedService<ServiceBusSessionReceiver<TSubscriber>>(_serviceKey);
/// <summary>
/// Adds a <b>singleton</b> Azure <see cref="ServiceBusReceiverHostedService{TReceiver}"/> keyed service that will be executed as a hosted service (i.e. in the background).
/// </summary>
/// <param name="serviceKey">The keyed singleton and health check key.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusReceiverHostedService{TReceiver}"/> instance.</param>
/// <returns>The <see cref="WithHostedServiceService{TReceiver}"/> instance for fluent-style method-chaining.</returns>
/// <remarks>No services are added until the chained <see cref="WithHostedServiceService{TReceiver}.Build"/> method is called.</remarks>
public WithHostedServiceService<ServiceBusSessionReceiver<TSubscriber>> WithHostedService(string serviceKey = "azure-service-bus-session-receiver", Action<IServiceProvider, ServiceBusReceiverHostedService<ServiceBusSessionReceiver<TSubscriber>>>? configure = null)
=> new(_owner.Services, serviceKey.ThrowIfNullOrEmpty(), configure, Build, GetReceiverInstance);
}
/// <summary>
/// Provides the <see cref="ServiceBusReceiverHostedService{TReceiver}"/> service registration.
/// </summary>
/// <typeparam name="TReceiver">The Azure <see cref="ServiceBusReceiverBase"/> <see cref="Type"/>.</typeparam>
public sealed class WithHostedServiceService<TReceiver> where TReceiver : ServiceBusReceiverBase
{
private readonly IServiceCollection _services;
private readonly string _serviceKey;
private readonly Action<IServiceProvider, ServiceBusReceiverHostedService<TReceiver>>? _configure;
private readonly Action _buildParentServices;
private readonly Func<IServiceProvider, object> _createReceiverInstance;
/// <summary>
/// Initializes a new instance of the <see cref="WithHostedServiceService{TReceiver}"/> class.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="serviceKey">The service key.</param>
/// <param name="configure">An optional action to configure the <see cref="ServiceBusReceiverHostedService{TReceiver}"/> instance.</param>
/// <param name="buildParentServices">The action to build the parent services (i.e. the subscriber and receiver).</param>
/// <param name="createReceiverInstance">The function to create the receiver instance.</param>
internal WithHostedServiceService(IServiceCollection services, string serviceKey, Action<IServiceProvider, ServiceBusReceiverHostedService<TReceiver>>? configure, Action buildParentServices, Func<IServiceProvider, object> createReceiverInstance)
{
_services = services.ThrowIfNull();
_serviceKey = serviceKey.ThrowIfNullOrEmpty();
_configure = configure;
_buildParentServices = buildParentServices;
_createReceiverInstance = createReceiverInstance;
}
/// <summary>
/// Builds and registers all of the chained services.
/// </summary>
public void Build()
{
// Adds the parent service registrations.
_buildParentServices();
// Adds the hosted service registration.
_services.AddHostedService(_serviceKey, sp =>
{
var receiver = _createReceiverInstance(sp);
return ActivatorUtilities.CreateInstance<ServiceBusReceiverHostedService<TReceiver>>(sp, receiver);
});
}
}
}
}