Skip to content

Commit 849602c

Browse files
committed
Check configuration from appsettings.json
1 parent db03377 commit 849602c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,22 @@ public MemcachedClientConfiguration(
4141
var options = optionsAccessor.Value;
4242
if ((options == null || options.Servers.Count == 0) && configuration != null)
4343
{
44-
configuration.GetSection("enyimMemcached").Bind(options);
44+
var section = configuration.GetSection("enyimMemcached");
45+
if (section.Exists())
46+
{
47+
section.Bind(options);
48+
}
49+
else
50+
{
51+
_logger.LogWarning($"No enyimMemcached setting in appsetting.json. Use default configuration");
52+
options.AddDefaultServer();
53+
}
4554
}
4655

4756
Servers = new List<DnsEndPoint>();
4857
foreach (var server in options.Servers)
4958
{
50-
Servers.Add(new DnsEndPoint(server.Address, server.Port));
59+
Servers.Add(new DnsEndPoint(server.Address, server.Port));
5160
}
5261

5362
SocketPool = new SocketPoolConfiguration();

Enyim.Caching/Configuration/MemcachedClientOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public void AddServer(string address, int port)
3030
Servers.Add(new Server { Address = address, Port = port });
3131
}
3232

33+
public void AddDefaultServer() => AddServer("memcached", 11211);
34+
3335
public void AddPlainTextAuthenticator(string zone, string userName, string password)
3436
{
3537
Authentication = new Authentication

Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
4949
throw new ArgumentNullException(nameof(configurationSection));
5050
}
5151

52+
if(!configurationSection.Exists())
53+
{
54+
throw new ArgumentNullException($"{configurationSection.Key} in appsettings.json");
55+
}
56+
5257
return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(configurationSection));
5358
}
5459

@@ -64,7 +69,13 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
6469
throw new ArgumentNullException(nameof(configuration));
6570
}
6671

67-
return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(configuration.GetSection(sectionKey)));
72+
var section = configuration.GetSection(sectionKey);
73+
if (!section.Exists())
74+
{
75+
throw new ArgumentNullException($"{sectionKey} in appsettings.json");
76+
}
77+
78+
return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(section));
6879
}
6980

7081
private static IServiceCollection AddEnyimMemcachedInternal(IServiceCollection services, Action<IServiceCollection> configure)
@@ -81,6 +92,6 @@ private static IServiceCollection AddEnyimMemcachedInternal(IServiceCollection s
8192
services.AddSingleton<IDistributedCache>(factory => factory.GetService<MemcachedClient>());
8293

8394
return services;
84-
}
95+
}
8596
}
8697
}

0 commit comments

Comments
 (0)