/
ServiceCollectionExtensions.cs
616 lines (578 loc) · 42.4 KB
/
ServiceCollectionExtensions.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace Cuemon.Extensions.DependencyInjection
{
/// <summary>
/// Extension methods for the <see cref="IServiceCollection"/> interface.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService, TImplementation>(this IServiceCollection services, ServiceLifetime lifetime)
where TService : class
where TImplementation : class, TService
{
return services.Add(typeof(TService), typeof(TImplementation), lifetime);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService, TImplementation, TOptions>(this IServiceCollection services, ServiceLifetime lifetime, Action<TOptions> setup)
where TService : class
where TImplementation : class, TService
where TOptions : class, new()
{
return services.Add(typeof(TService), typeof(TImplementation), lifetime, setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementation" /> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementation">The implementation type of the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add(this IServiceCollection services, Type service, Type implementation, ServiceLifetime lifetime)
{
Validator.ThrowIfNull(services);
services.AddServices(service, implementation, lifetime, false);
return services;
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementation" /> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementation">The implementation type of the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TOptions>(this IServiceCollection services, Type service, Type implementation, ServiceLifetime lifetime, Action<TOptions> setup)
where TOptions : class, new()
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(setup);
services.AddServices(service, implementation, lifetime, false);
services.Configure(setup);
return services;
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory, ServiceLifetime lifetime)
where TService : class
where TImplementation : class, TService
{
return services.Add(typeof(TService), implementationFactory, lifetime);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService, TImplementation, TOptions>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory, ServiceLifetime lifetime, Action<TOptions> setup)
where TService : class
where TImplementation : class, TService
where TOptions : class, new()
{
return services.Add(typeof(TService), implementationFactory, lifetime, setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementationFactory" /> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime)
{
Validator.ThrowIfNull(services);
services.AddServices(service, implementationFactory, lifetime, false);
return services;
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementationFactory" /> and <paramref name="lifetime"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TOptions>(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime, Action<TOptions> setup)
where TOptions : class, new()
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(setup);
services.AddServices(service, implementationFactory, lifetime, false);
services.Configure(setup);
return services;
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the configured <paramref name="setup"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
/// <remarks>If the underlying type of <typeparamref name="TService"/> implements <see cref="IDependencyInjectionMarker{TMarker}"/> interface then this is automatically handled.</remarks>
public static IServiceCollection Add<TService>(this IServiceCollection services, Action<TypeForwardServiceOptions> setup = null)
where TService : class
{
return Add<TService, TService>(services, setup);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and configured <paramref name="setup"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService, TImplementation>(this IServiceCollection services, Action<TypeForwardServiceOptions> setup = null)
where TService : class
where TImplementation : class, TService
{
return Add(services, typeof(TService), typeof(TImplementation), setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementation" /> and configured <paramref name="setup"/> to the <paramref name="services" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementation">The implementation type of the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add(this IServiceCollection services, Type service, Type implementation, Action<TypeForwardServiceOptions> setup = null)
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(service);
Validator.ThrowIfNull(implementation);
return AddServicesWithNestedTypeForwarding(services, service, implementation, setup, false);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the configured <paramref name="setup"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory, Action<TypeForwardServiceOptions> setup = null)
where TService : class
{
return Add<TService, TService>(services, implementationFactory, setup);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and configured <paramref name="setup"/> to the <paramref name="services" />.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory, Action<TypeForwardServiceOptions> setup = null)
where TService : class
where TImplementation : class, TService
{
return Add(services, typeof(TService), implementationFactory, setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementationFactory" /> and configured <paramref name="setup"/> to the <paramref name="services" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection Add(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, Action<TypeForwardServiceOptions> setup = null)
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(implementationFactory);
return AddServicesWithNestedTypeForwarding(services, service, implementationFactory, setup, false);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the configured <paramref name="setup"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
/// <remarks>If the underlying type of <typeparamref name="TService"/> implements <see cref="IDependencyInjectionMarker{TMarker}"/> interface then this is automatically handled.</remarks>
public static IServiceCollection TryAdd<TService>(this IServiceCollection services, Action<TypeForwardServiceOptions> setup = null)
where TService : class
{
return TryAdd<TService, TService>(services, setup);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and configured <paramref name="setup"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService, TImplementation>(this IServiceCollection services, Action<TypeForwardServiceOptions> setup = null)
where TService : class
where TImplementation : class, TService
{
return TryAdd(services, typeof(TService), typeof(TImplementation), setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementation" /> and configured <paramref name="setup"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementation">The implementation type of the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd(this IServiceCollection services, Type service, Type implementation, Action<TypeForwardServiceOptions> setup = null)
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(service);
Validator.ThrowIfNull(implementation);
return AddServicesWithNestedTypeForwarding(services, service, implementation, setup, true);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the configured <paramref name="setup"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory, Action<TypeForwardServiceOptions> setup = null)
where TService : class
{
return TryAdd<TService, TService>(services, implementationFactory, setup);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and configured <paramref name="setup"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory, Action<TypeForwardServiceOptions> setup = null)
where TService : class
where TImplementation : class, TService
{
return TryAdd(services, typeof(TService), implementationFactory, setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementationFactory" /> and configured <paramref name="setup"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="setup">The <see cref="TypeForwardServiceOptions" /> which may be configured.</param>
/// <returns>A reference to <paramref name="services"/> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, Action<TypeForwardServiceOptions> setup = null)
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(implementationFactory);
AddServicesWithNestedTypeForwarding(services, service, implementationFactory, setup, true);
return services;
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService, TImplementation>(this IServiceCollection services, ServiceLifetime lifetime)
where TService : class
where TImplementation : class, TService
{
Validator.ThrowIfNull(services);
return services.TryAdd(typeof(TService), typeof(TImplementation), lifetime);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService, TImplementation, TOptions>(this IServiceCollection services, ServiceLifetime lifetime, Action<TOptions> setup)
where TService : class
where TImplementation : class, TService
where TOptions : class, new()
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(setup);
return services.TryAdd(typeof(TService), typeof(TImplementation), lifetime, setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementation" /> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementation">The implementation type of the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd(this IServiceCollection services, Type service, Type implementation, ServiceLifetime lifetime)
{
Validator.ThrowIfNull(services);
services.AddServices(service, implementation, lifetime, true);
return services;
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementation" /> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementation">The implementation type of the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TOptions>(this IServiceCollection services, Type service, Type implementation, ServiceLifetime lifetime, Action<TOptions> setup)
where TOptions : class, new()
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(setup);
services.AddServices(service, implementation, lifetime, true);
services.Configure(setup);
return services;
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService, TImplementation>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory, ServiceLifetime lifetime)
where TService : class
where TImplementation : class, TService
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(implementationFactory);
return services.TryAdd(typeof(TService), implementationFactory, lifetime);
}
/// <summary>
/// Adds the specified <typeparamref name="TService"/> with the <typeparamref name="TImplementation"/> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TService, TImplementation, TOptions>(this IServiceCollection services, Func<IServiceProvider, TImplementation> implementationFactory, ServiceLifetime lifetime, Action<TOptions> setup)
where TService : class
where TImplementation : class, TService
where TOptions : class, new()
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(implementationFactory);
Validator.ThrowIfNull(setup);
return services.TryAdd(typeof(TService), implementationFactory, lifetime, setup);
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementationFactory" /> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime)
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(implementationFactory);
services.AddServices(service, implementationFactory, lifetime, true);
return services;
}
/// <summary>
/// Adds the specified <paramref name="service" /> with the <paramref name="implementationFactory" /> and <paramref name="lifetime"/> to the <paramref name="services" /> if the service type has not already been registered.
/// </summary>
/// <typeparam name="TOptions">The type of the configured options.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the service to.</param>
/// <param name="service">The type of the service to register.</param>
/// <param name="implementationFactory">The function delegate that creates the service.</param>
/// <param name="lifetime">The lifetime of the service.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryAdd<TOptions>(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime, Action<TOptions> setup)
where TOptions : class, new()
{
Validator.ThrowIfNull(services);
Validator.ThrowIfNull(implementationFactory);
Validator.ThrowIfNull(setup);
services.AddServices(service, implementationFactory, lifetime, true);
services.Configure(setup);
return services;
}
/// <summary>
/// Registers the specified <paramref name="setup" /> used to configure <typeparamref name="TOptions"/> to the <paramref name="services" /> if not already registered.
/// </summary>
/// <typeparam name="TOptions">The options type to be configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to add the <typeparamref name="TOptions"/> to.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
public static IServiceCollection TryConfigure<TOptions>(this IServiceCollection services, Action<TOptions> setup) where TOptions : class, new()
{
if (services != null && !services.Any(descriptor => descriptor.ServiceType == typeof(IConfigureOptions<TOptions>)))
{
services.Configure(setup);
}
return services;
}
/// <summary>
/// Registers an action used to post-configure all instances of a specific <typeparamref name="TOptions"/> type in the <paramref name="services"/> collection.
/// These are run after <see cref="OptionsServiceCollectionExtensions.Configure{TOptions}(IServiceCollection,Action{TOptions})"/>.
/// </summary>
/// <typeparam name="TOptions">The options type to be configured.</typeparam>
/// <param name="services">The <see cref="IServiceCollection" /> to extend.</param>
/// <param name="setup">The <typeparamref name="TOptions"/> which need to be configured.</param>
/// <returns>A reference to <paramref name="services" /> so that additional configuration calls can be chained.</returns>
/// <remarks>Iterates over all the services in the collection, and if the service is of type <see cref="IConfigureOptions{TOptions}"/> and its generic type argument matches the specified <typeparamref name="TOptions"/> type, it creates a new instance of <see cref="PostConfigureOptions{TOptions}"/> for that service and adds it to the <paramref name="services"/> collection.</remarks>
public static IServiceCollection PostConfigureAllOf<TOptions>(this IServiceCollection services, Action<TOptions> setup) where TOptions : class
{
var baseOptionsType = typeof(TOptions);
var configureOptionsType = typeof(IConfigureOptions<>);
var options = services.Where(descriptor =>
{
if (Decorator.Enclose(descriptor.ServiceType).HasInterfaces(configureOptionsType))
{
return baseOptionsType.IsInterface
? Decorator.Enclose(descriptor.ServiceType.GenericTypeArguments[0]).HasInterfaces(baseOptionsType)
: Decorator.Enclose(descriptor.ServiceType.GenericTypeArguments[0]).HasTypes(baseOptionsType);
}
return false;
}).ToList();
foreach (var option in options)
{
var instance = option.ImplementationInstance;
if (instance != null)
{
var instanceType = instance.GetType();
if (instanceType.IsGenericType)
{
var optionType = instanceType.GenericTypeArguments[0];
if (instanceType == typeof(ConfigureNamedOptions<>).MakeGenericType(optionType))
{
var name = instanceType.GetProperty(nameof(ConfigureNamedOptions<TOptions>.Name))!.GetValue(instance);
var postConfigureOptionsType = typeof(IPostConfigureOptions<>).MakeGenericType(optionType);
var postConfigureOptions = Activator.CreateInstance(
typeof(PostConfigureOptions<>).MakeGenericType(optionType),
BindingFlags.Instance | BindingFlags.Public,
binder: null,
args: new[] { name, setup },
culture: null);
services.AddSingleton(postConfigureOptionsType, postConfigureOptions!);
}
}
}
}
return services;
}
private static void AddServices(this IServiceCollection services, Type service, Type implementation, ServiceLifetime lifetime, bool useTesterDoerPattern)
{
switch (lifetime)
{
case ServiceLifetime.Scoped:
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAddScoped(service, implementation), () => services.AddScoped(service, implementation));
break;
case ServiceLifetime.Singleton:
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAddSingleton(service, implementation), () => services.AddSingleton(service, implementation));
break;
case ServiceLifetime.Transient:
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAddTransient(service, implementation), () => services.AddTransient(service, implementation));
break;
}
}
private static void AddServices(this IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime, bool useTesterDoerPattern)
{
switch (lifetime)
{
case ServiceLifetime.Scoped:
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAddScoped(service, implementationFactory), () => services.AddScoped(service, implementationFactory));
break;
case ServiceLifetime.Singleton:
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAddSingleton(service, implementationFactory), () => services.AddSingleton(service, implementationFactory));
break;
case ServiceLifetime.Transient:
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAddTransient(service, implementationFactory), () => services.AddTransient(service, implementationFactory));
break;
}
}
private static IServiceCollection AddServicesWithNestedTypeForwarding(IServiceCollection services, Type service, Type implementation, Action<TypeForwardServiceOptions> setup, bool useTesterDoerPattern)
{
var options = Patterns.Configure(setup);
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAdd(service, implementation, options.Lifetime), () => services.Add(service, implementation, options.Lifetime));
if (options.UseNestedTypeForwarding) { AddServicesWithNestedTypeForwarding(services, service, options, useTesterDoerPattern); }
return services;
}
private static IServiceCollection AddServicesWithNestedTypeForwarding(IServiceCollection services, Type service, Func<IServiceProvider, object> implementationFactory, Action<TypeForwardServiceOptions> setup, bool useTesterDoerPattern)
{
var options = Patterns.Configure(setup);
Condition.FlipFlop(useTesterDoerPattern, () => services.TryAdd(service, implementationFactory, options.Lifetime), () => services.Add(service, implementationFactory, options.Lifetime));
if (options.UseNestedTypeForwarding) { AddServicesWithNestedTypeForwarding(services, service, options, useTesterDoerPattern); }
return services;
}
private static void AddServicesWithNestedTypeForwarding(IServiceCollection services, Type service, TypeForwardServiceOptions options, bool useTesterDoerPattern)
{
var hasMarkerType = service.TryGetDependencyInjectionMarker(out _);
foreach (var groupingTypes in options.NestedTypeSelector(service).Where(type => options.NestedTypePredicate(type)).GroupBy(type => Decorator.Enclose(type).ToFriendlyName(o => o.ExcludeGenericArguments = true)))
{
var orderedGroupingTypes = groupingTypes.OrderBy(type => type.Name, StringComparer.InvariantCulture);
if (useTesterDoerPattern)
{
TryAdd(services, hasMarkerType ? orderedGroupingTypes.Last() : orderedGroupingTypes.First(), p => p.GetRequiredService(service), options.Lifetime);
}
else
{
Add(services, hasMarkerType ? orderedGroupingTypes.Last() : orderedGroupingTypes.First(), p => p.GetRequiredService(service), options.Lifetime);
}
}
}
}
}