@@ -13,18 +13,21 @@ namespace Microsoft.Extensions.DependencyInjection
13
13
{
14
14
public static class EnyimMemcachedServiceCollectionExtensions
15
15
{
16
- /// <summary>
17
- /// Add EnyimMemcached to the specified <see cref="IServiceCollection"/>.
18
- /// Read configuration via IConfiguration.GetSection("enyimMemcached")
19
- /// </summary>
20
- /// <param name="services"></param>
21
- /// <returns></returns>
22
- public static IServiceCollection AddEnyimMemcached ( this IServiceCollection services )
16
+ #if NET6_0_OR_GREATER
17
+ public static IServiceCollection AddEnyimMemcached (
18
+ this IServiceCollection services ,
19
+ string sectionKey = "enyimMemcached" ,
20
+ bool asDistributedCache = true )
23
21
{
24
- return AddEnyimMemcachedInternal ( services , null ) ;
22
+ var config = services . BuildServiceProvider ( ) . GetRequiredService < IConfiguration > ( ) ;
23
+ return services . AddEnyimMemcached ( config . GetSection ( sectionKey ) , asDistributedCache ) ;
25
24
}
25
+ #endif
26
26
27
- public static IServiceCollection AddEnyimMemcached ( this IServiceCollection services , Action < MemcachedClientOptions > setupAction )
27
+ public static IServiceCollection AddEnyimMemcached (
28
+ this IServiceCollection services ,
29
+ Action < MemcachedClientOptions > setupAction ,
30
+ bool asDistributedCache = true )
28
31
{
29
32
if ( services == null )
30
33
{
@@ -36,10 +39,13 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
36
39
throw new ArgumentNullException ( nameof ( setupAction ) ) ;
37
40
}
38
41
39
- return AddEnyimMemcachedInternal ( services , s => s . Configure ( setupAction ) ) ;
42
+ return services . AddEnyimMemcachedInternal ( s => s . Configure ( setupAction ) , asDistributedCache ) ;
40
43
}
41
44
42
- public static IServiceCollection AddEnyimMemcached ( this IServiceCollection services , IConfigurationSection configurationSection )
45
+ public static IServiceCollection AddEnyimMemcached (
46
+ this IServiceCollection services ,
47
+ IConfigurationSection configurationSection ,
48
+ bool asDistributedCache )
43
49
{
44
50
if ( services == null )
45
51
{
@@ -56,10 +62,14 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
56
62
throw new ArgumentNullException ( $ "{ configurationSection . Key } in appsettings.json") ;
57
63
}
58
64
59
- return AddEnyimMemcachedInternal ( services , s => s . Configure < MemcachedClientOptions > ( configurationSection ) ) ;
65
+ return services . AddEnyimMemcachedInternal ( s => s . Configure < MemcachedClientOptions > ( configurationSection ) , asDistributedCache ) ;
60
66
}
61
67
62
- public static IServiceCollection AddEnyimMemcached ( this IServiceCollection services , IConfiguration configuration , string sectionKey = "enyimMemcached" )
68
+ public static IServiceCollection AddEnyimMemcached (
69
+ this IServiceCollection services ,
70
+ IConfiguration configuration ,
71
+ string sectionKey = "enyimMemcached" ,
72
+ bool asDistributedCache = true )
63
73
{
64
74
if ( services == null )
65
75
{
@@ -77,25 +87,41 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
77
87
throw new ArgumentNullException ( $ "{ sectionKey } in appsettings.json") ;
78
88
}
79
89
80
- return AddEnyimMemcachedInternal ( services , s => s . Configure < MemcachedClientOptions > ( section ) ) ;
90
+ return services . AddEnyimMemcachedInternal ( s => s . Configure < MemcachedClientOptions > ( section ) , asDistributedCache ) ;
81
91
}
82
92
83
- private static IServiceCollection AddEnyimMemcachedInternal ( IServiceCollection services , Action < IServiceCollection > configure )
93
+ private static IServiceCollection AddEnyimMemcachedInternal (
94
+ this IServiceCollection services ,
95
+ Action < IServiceCollection > configure ,
96
+ bool asDistributedCache )
84
97
{
85
98
services . AddOptions ( ) ;
86
99
configure ? . Invoke ( services ) ;
87
100
88
101
services . TryAddSingleton < ITranscoder , DefaultTranscoder > ( ) ;
89
102
services . TryAddSingleton < IMemcachedKeyTransformer , DefaultKeyTransformer > ( ) ;
90
103
services . TryAddSingleton < IMemcachedClientConfiguration , MemcachedClientConfiguration > ( ) ;
91
- services . AddSingleton < MemcachedClient > ( ) ;
104
+ services . TryAddSingleton < IMemcachedClient , MemcachedClient > ( ) ;
92
105
93
- services . AddSingleton < IMemcachedClient > ( factory => factory . GetService < MemcachedClient > ( ) ) ;
94
- services . AddSingleton < IDistributedCache > ( factory => factory . GetService < MemcachedClient > ( ) ) ;
106
+ if ( asDistributedCache )
107
+ {
108
+ services . TryAddSingleton < IDistributedCache > ( sp =>
109
+ sp . GetRequiredService < IMemcachedClient > ( ) as MemcachedClient ) ;
110
+ }
95
111
96
112
return services ;
97
113
}
98
114
115
+ #if NET6_0_OR_GREATER
116
+ public static IServiceCollection AddEnyimMemcached < T > (
117
+ this IServiceCollection services ,
118
+ string sectionKey )
119
+ {
120
+ var config = services . BuildServiceProvider ( ) . GetRequiredService < IConfiguration > ( ) ;
121
+ return services . AddEnyimMemcached < T > ( config , sectionKey ) ;
122
+ }
123
+ #endif
124
+
99
125
public static IServiceCollection AddEnyimMemcached < T > (
100
126
this IServiceCollection services ,
101
127
IConfiguration configuration ,
@@ -106,7 +132,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
106
132
services . TryAddSingleton < ITranscoder , DefaultTranscoder > ( ) ;
107
133
services . TryAddSingleton < IMemcachedKeyTransformer , DefaultKeyTransformer > ( ) ;
108
134
109
- services . AddSingleton < IMemcachedClient < T > > ( sp =>
135
+ services . TryAddSingleton < IMemcachedClient < T > > ( sp =>
110
136
{
111
137
var loggerFactory = sp . GetRequiredService < ILoggerFactory > ( ) ;
112
138
var options = sp . GetRequiredService < IOptionsMonitor < MemcachedClientOptions > > ( ) ;
0 commit comments