Skip to content

Commit 5c3f83b

Browse files
authored
Merge pull request #192 from cnblogs/remove-buildserviceprovider
refactor: remove services.BuildServiceProvider
2 parents 81ff448 + f7ffce4 commit 5c3f83b

File tree

1 file changed

+59
-16
lines changed

1 file changed

+59
-16
lines changed

src/Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,30 @@
88
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Options;
1010
using System;
11+
using static System.Collections.Specialized.BitVector32;
1112

1213
namespace Microsoft.Extensions.DependencyInjection
1314
{
1415
public static class EnyimMemcachedServiceCollectionExtensions
1516
{
16-
#if NET6_0_OR_GREATER
17-
[Obsolete("Calling BuildServiceProvider has side affects")]
1817
public static IServiceCollection AddEnyimMemcached(
1918
this IServiceCollection services,
2019
string sectionKey = "enyimMemcached",
2120
bool asDistributedCache = true)
2221
{
23-
var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
24-
config[""] = "";
25-
return services.AddEnyimMemcached(config.GetSection(sectionKey), asDistributedCache);
22+
if (services == null)
23+
{
24+
throw new ArgumentNullException(nameof(services));
25+
}
26+
27+
if (string.IsNullOrEmpty(sectionKey))
28+
{
29+
throw new ArgumentNullException(nameof(sectionKey));
30+
}
31+
32+
return services.AddEnyimMemcachedInternal(
33+
s => s.AddOptions<MemcachedClientOptions>().BindConfiguration(sectionKey), asDistributedCache);
2634
}
27-
#endif
2835

2936
public static IServiceCollection AddEnyimMemcached(
3037
this IServiceCollection services,
@@ -41,7 +48,8 @@ public static IServiceCollection AddEnyimMemcached(
4148
throw new ArgumentNullException(nameof(setupAction));
4249
}
4350

44-
return services.AddEnyimMemcachedInternal(s => s.Configure(setupAction), asDistributedCache);
51+
return services.AddEnyimMemcachedInternal(
52+
s => s.Configure(setupAction), asDistributedCache);
4553
}
4654

4755
public static IServiceCollection AddEnyimMemcached(
@@ -64,7 +72,8 @@ public static IServiceCollection AddEnyimMemcached(
6472
throw new ArgumentNullException($"{configurationSection.Key} in appsettings.json");
6573
}
6674

67-
return services.AddEnyimMemcachedInternal(s => s.Configure<MemcachedClientOptions>(configurationSection), asDistributedCache);
75+
return services.AddEnyimMemcachedInternal(
76+
s => s.Configure<MemcachedClientOptions>(configurationSection), asDistributedCache);
6877
}
6978

7079
public static IServiceCollection AddEnyimMemcached(
@@ -89,7 +98,8 @@ public static IServiceCollection AddEnyimMemcached(
8998
throw new ArgumentNullException($"{sectionKey} in appsettings.json");
9099
}
91100

92-
return services.AddEnyimMemcachedInternal(s => s.Configure<MemcachedClientOptions>(section), asDistributedCache);
101+
return services.AddEnyimMemcachedInternal(
102+
s => s.Configure<MemcachedClientOptions>(section), asDistributedCache);
93103
}
94104

95105
private static IServiceCollection AddEnyimMemcachedInternal(
@@ -114,31 +124,64 @@ private static IServiceCollection AddEnyimMemcachedInternal(
114124
return services;
115125
}
116126

117-
#if NET6_0_OR_GREATER
118127
public static IServiceCollection AddEnyimMemcached<T>(
119128
this IServiceCollection services,
120129
string sectionKey)
121130
{
122-
var config = services.BuildServiceProvider().GetRequiredService<IConfiguration>();
123-
return services.AddEnyimMemcached<T>(config, sectionKey);
131+
if (services == null)
132+
{
133+
throw new ArgumentNullException(nameof(services));
134+
}
135+
136+
if (string.IsNullOrEmpty(sectionKey))
137+
{
138+
throw new ArgumentNullException(nameof(sectionKey));
139+
}
140+
141+
return services.AddEnyimMemcached<T>(
142+
s => s.AddOptions<MemcachedClientOptions>().BindConfiguration(sectionKey));
124143
}
125-
#endif
126144

127145
public static IServiceCollection AddEnyimMemcached<T>(
128146
this IServiceCollection services,
129147
IConfiguration configuration,
130148
string sectionKey)
149+
{
150+
if (services == null)
151+
{
152+
throw new ArgumentNullException(nameof(services));
153+
}
154+
155+
if (configuration == null)
156+
{
157+
throw new ArgumentNullException(nameof(configuration));
158+
}
159+
160+
var section = configuration.GetSection(sectionKey);
161+
if (!section.Exists())
162+
{
163+
throw new ArgumentNullException($"{sectionKey} in appsettings.json");
164+
}
165+
166+
return services.AddEnyimMemcached<T>(
167+
s => s.Configure<MemcachedClientOptions>(configuration.GetSection(sectionKey)));
168+
}
169+
170+
public static IServiceCollection AddEnyimMemcached<T>(
171+
this IServiceCollection services,
172+
Action<IServiceCollection> configure)
131173
{
132174
services.AddOptions();
133-
services.Configure<MemcachedClientOptions>(sectionKey, configuration.GetSection(sectionKey));
175+
configure?.Invoke(services);
176+
134177
services.TryAddSingleton<ITranscoder, DefaultTranscoder>();
135178
services.TryAddSingleton<IMemcachedKeyTransformer, DefaultKeyTransformer>();
136179

137180
services.TryAddSingleton<IMemcachedClient<T>>(sp =>
138181
{
139182
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
140-
var options = sp.GetRequiredService<IOptionsMonitor<MemcachedClientOptions>>();
141-
var conf = new MemcachedClientConfiguration(loggerFactory, options.Get(sectionKey));
183+
var options = sp.GetRequiredService<IOptions<MemcachedClientOptions>>();
184+
var conf = new MemcachedClientConfiguration(loggerFactory, options);
142185
return new MemcachedClient<T>(loggerFactory, conf);
143186
});
144187

0 commit comments

Comments
 (0)