-
Notifications
You must be signed in to change notification settings - Fork 114
/
ServiceCollectionExtensions.cs
176 lines (147 loc) · 8.86 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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
//
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.FeatureManagement.FeatureFilters;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.FeatureManagement
{
/// <summary>
/// Extensions used to add feature management functionality.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds singleton <see cref="FeatureManager"/> and other required feature management services.
/// </summary>
/// <param name="services">The service collection that feature management services are added to.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
/// <exception cref="FeatureManagementException">Thrown if <see cref="FeatureManager"/> has been registered as scoped.</exception>
public static IFeatureManagementBuilder AddFeatureManagement(this IServiceCollection services)
{
if (services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
{
throw new FeatureManagementException(
FeatureManagementError.Conflict,
"Scoped feature management has been registered.");
}
AddCommonFeatureManagementServices(services);
services.AddSingleton(sp => new FeatureManager(
sp.GetRequiredService<IFeatureDefinitionProvider>(),
sp.GetRequiredService<IOptions<FeatureManagementOptions>>().Value)
{
FeatureFilters = sp.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>(),
SessionManagers = sp.GetRequiredService<IEnumerable<ISessionManager>>(),
Cache = sp.GetRequiredService<IMemoryCache>(),
Logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger<FeatureManager>(),
TargetingContextAccessor = sp.GetService<ITargetingContextAccessor>(),
AssignerOptions = sp.GetRequiredService<IOptions<TargetingEvaluationOptions>>().Value
});
services.TryAddSingleton<IFeatureManager>(sp => sp.GetRequiredService<FeatureManager>());
services.TryAddSingleton<IVariantFeatureManager>(sp => sp.GetRequiredService<FeatureManager>());
return GetFeatureManagementBuilder(services);
}
/// <summary>
/// Adds singleton <see cref="FeatureManager"/> and other required feature management services.
/// The registered <see cref="ConfigurationFeatureDefinitionProvider"/> will use the provided configuration and read from the top level if no "FeatureManagement" section can be found.
/// </summary>
/// <param name="services">The service collection that feature management services are added to.</param>
/// <param name="configuration">A specific <see cref="IConfiguration"/> instance that will be used to obtain feature settings.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder AddFeatureManagement(this IServiceCollection services, IConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
services.AddSingleton<IFeatureDefinitionProvider>(sp =>
new ConfigurationFeatureDefinitionProvider(configuration)
{
RootConfigurationFallbackEnabled = true,
Logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger<ConfigurationFeatureDefinitionProvider>()
});
return services.AddFeatureManagement();
}
/// <summary>
/// Adds scoped <see cref="FeatureManager"/> and other required feature management services.
/// </summary>
/// <param name="services">The service collection that feature management services are added to.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
/// <exception cref="FeatureManagementException">Thrown if <see cref="FeatureManager"/> has been registered as singleton.</exception>
public static IFeatureManagementBuilder AddScopedFeatureManagement(this IServiceCollection services)
{
if (services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Singleton))
{
throw new FeatureManagementException(
FeatureManagementError.Conflict,
"Singleton feature management has been registered.");
}
AddCommonFeatureManagementServices(services);
services.AddScoped(sp => new FeatureManager(
sp.GetRequiredService<IFeatureDefinitionProvider>(),
sp.GetRequiredService<IOptions<FeatureManagementOptions>>().Value)
{
FeatureFilters = sp.GetRequiredService<IEnumerable<IFeatureFilterMetadata>>(),
SessionManagers = sp.GetRequiredService<IEnumerable<ISessionManager>>(),
Cache = sp.GetRequiredService<IMemoryCache>(),
Logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger<FeatureManager>(),
TargetingContextAccessor = sp.GetService<ITargetingContextAccessor>(),
AssignerOptions = sp.GetRequiredService<IOptions<TargetingEvaluationOptions>>().Value
});
services.TryAddScoped<IFeatureManager>(sp => sp.GetRequiredService<FeatureManager>());
services.TryAddScoped<IVariantFeatureManager>(sp => sp.GetRequiredService<FeatureManager>());
return GetFeatureManagementBuilder(services);
}
/// <summary>
/// Adds scoped <see cref="FeatureManager"/> and other required feature management services.
/// The registered <see cref="ConfigurationFeatureDefinitionProvider"/> will use the provided configuration and read from the top level if no "FeatureManagement" section can be found.
/// </summary>
/// <param name="services">The service collection that feature management services are added to.</param>
/// <param name="configuration">A specific <see cref="IConfiguration"/> instance that will be used to obtain feature settings.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder AddScopedFeatureManagement(this IServiceCollection services, IConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
services.AddSingleton<IFeatureDefinitionProvider>(sp =>
new ConfigurationFeatureDefinitionProvider(configuration)
{
RootConfigurationFallbackEnabled = true,
Logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger<ConfigurationFeatureDefinitionProvider>()
});
return services.AddScopedFeatureManagement();
}
private static void AddCommonFeatureManagementServices(IServiceCollection services)
{
services.AddLogging();
services.AddMemoryCache();
services.TryAddSingleton<IFeatureDefinitionProvider, ConfigurationFeatureDefinitionProvider>();
services.AddScoped<FeatureManagerSnapshot>();
services.TryAddScoped<IFeatureManagerSnapshot>(sp => sp.GetRequiredService<FeatureManagerSnapshot>());
services.TryAddScoped<IVariantFeatureManagerSnapshot>(sp => sp.GetRequiredService<FeatureManagerSnapshot>());
}
private static IFeatureManagementBuilder GetFeatureManagementBuilder(IServiceCollection services)
{
var builder = new FeatureManagementBuilder(services);
//
// Add built-in feature filters
builder.AddFeatureFilter<PercentageFilter>();
builder.AddFeatureFilter<TimeWindowFilter>(sp =>
new TimeWindowFilter()
{
Cache = sp.GetRequiredService<IMemoryCache>()
});
builder.AddFeatureFilter<ContextualTargetingFilter>();
return builder;
}
}
}