-
Notifications
You must be signed in to change notification settings - Fork 751
/
StartupInitializationBuilder.cs
71 lines (59 loc) · 2.42 KB
/
StartupInitializationBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options.Validation;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.Hosting.Testing.Internal;
/// <summary>
/// Builds server initialization phase.
/// </summary>
internal sealed class StartupInitializationBuilder : IStartupInitializationBuilder
{
/// <summary>
/// Gets services used to configure initializers.
/// </summary>
public IServiceCollection Services { get; }
/// <summary>
/// Initializes a new instance of the <see cref="StartupInitializationBuilder"/> class.
/// </summary>
/// <param name="services">Service collection used for configuration.</param>
public StartupInitializationBuilder(IServiceCollection services)
{
Services = services;
RegisterHostedService(services);
_ = Services.AddValidatedOptions<StartupInitializationOptions, StartupInitializationOptionsValidator>();
}
/// <summary>
/// Adds initializer of given type to be executed at service startup.
/// </summary>
/// <typeparam name="T">Type of the initializer to add.</typeparam>
/// <returns>Instance of <see cref="StartupInitializationBuilder"/> for further configuration.</returns>
public IStartupInitializationBuilder AddInitializer<T>()
where T : class, IStartupInitializer
{
Services.TryAddTransient<IStartupInitializer, T>();
return this;
}
/// <inheritdoc/>
public IStartupInitializationBuilder AddInitializer(Func<IServiceProvider, CancellationToken, Task> initializer)
{
_ = Throw.IfNull(initializer);
_ = Services.AddTransient<IStartupInitializer>(provider => new FunctionDerivedInitializer(provider, initializer));
return this;
}
private static void RegisterHostedService(IServiceCollection services)
{
if (services.Count != 0 && services[0].ImplementationType == typeof(StartupHostedService))
{
return;
}
services
.RemoveAll<StartupHostedService>()
.Insert(0, ServiceDescriptor.Singleton<IHostedService, StartupHostedService>());
}
}