@@ -13,18 +13,21 @@ namespace Microsoft.Extensions.DependencyInjection
1313{
1414 public static class EnyimMemcachedServiceCollectionExtensions
1515 {
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 )
2321 {
24- return AddEnyimMemcachedInternal ( services , null ) ;
22+ var config = services . BuildServiceProvider ( ) . GetRequiredService < IConfiguration > ( ) ;
23+ return services . AddEnyimMemcached ( config . GetSection ( sectionKey ) , asDistributedCache ) ;
2524 }
25+ #endif
2626
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 )
2831 {
2932 if ( services == null )
3033 {
@@ -36,10 +39,13 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
3639 throw new ArgumentNullException ( nameof ( setupAction ) ) ;
3740 }
3841
39- return AddEnyimMemcachedInternal ( services , s => s . Configure ( setupAction ) ) ;
42+ return services . AddEnyimMemcachedInternal ( s => s . Configure ( setupAction ) , asDistributedCache ) ;
4043 }
4144
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 )
4349 {
4450 if ( services == null )
4551 {
@@ -56,10 +62,14 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
5662 throw new ArgumentNullException ( $ "{ configurationSection . Key } in appsettings.json") ;
5763 }
5864
59- return AddEnyimMemcachedInternal ( services , s => s . Configure < MemcachedClientOptions > ( configurationSection ) ) ;
65+ return services . AddEnyimMemcachedInternal ( s => s . Configure < MemcachedClientOptions > ( configurationSection ) , asDistributedCache ) ;
6066 }
6167
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 )
6373 {
6474 if ( services == null )
6575 {
@@ -77,25 +87,41 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
7787 throw new ArgumentNullException ( $ "{ sectionKey } in appsettings.json") ;
7888 }
7989
80- return AddEnyimMemcachedInternal ( services , s => s . Configure < MemcachedClientOptions > ( section ) ) ;
90+ return services . AddEnyimMemcachedInternal ( s => s . Configure < MemcachedClientOptions > ( section ) , asDistributedCache ) ;
8191 }
8292
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 )
8497 {
8598 services . AddOptions ( ) ;
8699 configure ? . Invoke ( services ) ;
87100
88101 services . TryAddSingleton < ITranscoder , DefaultTranscoder > ( ) ;
89102 services . TryAddSingleton < IMemcachedKeyTransformer , DefaultKeyTransformer > ( ) ;
90103 services . TryAddSingleton < IMemcachedClientConfiguration , MemcachedClientConfiguration > ( ) ;
91- services . AddSingleton < MemcachedClient > ( ) ;
104+ services . TryAddSingleton < IMemcachedClient , MemcachedClient > ( ) ;
92105
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+ }
95111
96112 return services ;
97113 }
98114
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+
99125 public static IServiceCollection AddEnyimMemcached < T > (
100126 this IServiceCollection services ,
101127 IConfiguration configuration ,
@@ -106,7 +132,7 @@ public static IServiceCollection AddEnyimMemcached<T>(
106132 services . TryAddSingleton < ITranscoder , DefaultTranscoder > ( ) ;
107133 services . TryAddSingleton < IMemcachedKeyTransformer , DefaultKeyTransformer > ( ) ;
108134
109- services . AddSingleton < IMemcachedClient < T > > ( sp =>
135+ services . TryAddSingleton < IMemcachedClient < T > > ( sp =>
110136 {
111137 var loggerFactory = sp . GetRequiredService < ILoggerFactory > ( ) ;
112138 var options = sp . GetRequiredService < IOptionsMonitor < MemcachedClientOptions > > ( ) ;
0 commit comments