-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
StartupBase.cs
81 lines (71 loc) · 3.02 KB
/
StartupBase.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Hosting;
/// <summary>
/// Base class for initializing services and middlewares used by an application.
/// </summary>
public abstract class StartupBase : IStartup
{
/// <summary>
/// Configures the application.
/// </summary>
/// <param name="app">An <see cref="IApplicationBuilder"/> for the app to configure.</param>
public abstract void Configure(IApplicationBuilder app);
IServiceProvider IStartup.ConfigureServices(IServiceCollection services)
{
ConfigureServices(services);
return CreateServiceProvider(services);
}
/// <summary>
/// Register services into the <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
public virtual void ConfigureServices(IServiceCollection services)
{
}
/// <summary>
/// Creates an <see cref="IServiceProvider" /> instance for a given <see cref="ConfigureServices(IServiceCollection)" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceProvider"/>.</returns>
public virtual IServiceProvider CreateServiceProvider(IServiceCollection services)
{
return services.BuildServiceProvider();
}
}
/// <summary>
/// Base class for initializing services and middlewares used for configuring a <typeparamref name="TBuilder"/>.
/// </summary>
/// <typeparam name="TBuilder">The type of builder associated with the startup configuration.</typeparam>
public abstract class StartupBase<TBuilder> : StartupBase where TBuilder : notnull
{
private readonly IServiceProviderFactory<TBuilder> _factory;
/// <summary>
/// Constructor for StartupBase class.
/// </summary>
/// <param name="factory">A factory used to generate <see cref="IServiceProvider"/> instances.</param>
public StartupBase(IServiceProviderFactory<TBuilder> factory)
{
_factory = factory;
}
/// <summary>
/// Creates an <see cref="IServiceProvider" /> instance for a given <see cref="IServiceCollection" />.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceProvider"/>.</returns>
public override IServiceProvider CreateServiceProvider(IServiceCollection services)
{
var builder = _factory.CreateBuilder(services);
ConfigureContainer(builder);
return _factory.CreateServiceProvider(builder);
}
/// <summary>
/// Sets up the service container.
/// </summary>
/// <param name="builder">The builder associated with the container to configure.</param>
public virtual void ConfigureContainer(TBuilder builder)
{
}
}