8
8
using Microsoft . Extensions . Logging ;
9
9
using Microsoft . Extensions . Options ;
10
10
using System ;
11
+ using static System . Collections . Specialized . BitVector32 ;
11
12
12
13
namespace Microsoft . Extensions . DependencyInjection
13
14
{
14
15
public static class EnyimMemcachedServiceCollectionExtensions
15
16
{
16
- #if NET6_0_OR_GREATER
17
- [ Obsolete ( "Calling BuildServiceProvider has side affects" ) ]
18
17
public static IServiceCollection AddEnyimMemcached (
19
18
this IServiceCollection services ,
20
19
string sectionKey = "enyimMemcached" ,
21
20
bool asDistributedCache = true )
22
21
{
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 ) ;
26
34
}
27
- #endif
28
35
29
36
public static IServiceCollection AddEnyimMemcached (
30
37
this IServiceCollection services ,
@@ -41,7 +48,8 @@ public static IServiceCollection AddEnyimMemcached(
41
48
throw new ArgumentNullException ( nameof ( setupAction ) ) ;
42
49
}
43
50
44
- return services . AddEnyimMemcachedInternal ( s => s . Configure ( setupAction ) , asDistributedCache ) ;
51
+ return services . AddEnyimMemcachedInternal (
52
+ s => s . Configure ( setupAction ) , asDistributedCache ) ;
45
53
}
46
54
47
55
public static IServiceCollection AddEnyimMemcached (
@@ -64,7 +72,8 @@ public static IServiceCollection AddEnyimMemcached(
64
72
throw new ArgumentNullException ( $ "{ configurationSection . Key } in appsettings.json") ;
65
73
}
66
74
67
- return services . AddEnyimMemcachedInternal ( s => s . Configure < MemcachedClientOptions > ( configurationSection ) , asDistributedCache ) ;
75
+ return services . AddEnyimMemcachedInternal (
76
+ s => s . Configure < MemcachedClientOptions > ( configurationSection ) , asDistributedCache ) ;
68
77
}
69
78
70
79
public static IServiceCollection AddEnyimMemcached (
@@ -89,7 +98,8 @@ public static IServiceCollection AddEnyimMemcached(
89
98
throw new ArgumentNullException ( $ "{ sectionKey } in appsettings.json") ;
90
99
}
91
100
92
- return services . AddEnyimMemcachedInternal ( s => s . Configure < MemcachedClientOptions > ( section ) , asDistributedCache ) ;
101
+ return services . AddEnyimMemcachedInternal (
102
+ s => s . Configure < MemcachedClientOptions > ( section ) , asDistributedCache ) ;
93
103
}
94
104
95
105
private static IServiceCollection AddEnyimMemcachedInternal (
@@ -114,31 +124,64 @@ private static IServiceCollection AddEnyimMemcachedInternal(
114
124
return services ;
115
125
}
116
126
117
- #if NET6_0_OR_GREATER
118
127
public static IServiceCollection AddEnyimMemcached < T > (
119
128
this IServiceCollection services ,
120
129
string sectionKey )
121
130
{
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 ) ) ;
124
143
}
125
- #endif
126
144
127
145
public static IServiceCollection AddEnyimMemcached < T > (
128
146
this IServiceCollection services ,
129
147
IConfiguration configuration ,
130
148
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 )
131
173
{
132
174
services . AddOptions ( ) ;
133
- services . Configure < MemcachedClientOptions > ( sectionKey , configuration . GetSection ( sectionKey ) ) ;
175
+ configure ? . Invoke ( services ) ;
176
+
134
177
services . TryAddSingleton < ITranscoder , DefaultTranscoder > ( ) ;
135
178
services . TryAddSingleton < IMemcachedKeyTransformer , DefaultKeyTransformer > ( ) ;
136
179
137
180
services . TryAddSingleton < IMemcachedClient < T > > ( sp =>
138
181
{
139
182
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 ) ;
142
185
return new MemcachedClient < T > ( loggerFactory , conf ) ;
143
186
} ) ;
144
187
0 commit comments