|
7 | 7 | using Microsoft.Extensions.Logging;
|
8 | 8 | using Microsoft.Extensions.Options;
|
9 | 9 | using Microsoft.Extensions.Configuration;
|
| 10 | +using System.Linq; |
| 11 | +using System.Net.Sockets; |
10 | 12 |
|
11 | 13 | namespace Enyim.Caching.Configuration
|
12 | 14 | {
|
@@ -41,23 +43,19 @@ public MemcachedClientConfiguration(
|
41 | 43 | var options = optionsAccessor.Value;
|
42 | 44 | if ((options == null || options.Servers.Count == 0) && configuration != null)
|
43 | 45 | {
|
44 |
| - var section = configuration.GetSection("enyimMemcached"); |
| 46 | + var section = configuration.GetSection("enyimMemcached"); |
45 | 47 | if (section.Exists())
|
46 | 48 | {
|
47 |
| - section.Bind(options); |
| 49 | + section.Bind(options); |
48 | 50 | }
|
49 | 51 | else
|
50 | 52 | {
|
51 | 53 | _logger.LogWarning($"No enyimMemcached setting in appsetting.json. Use default configuration");
|
52 | 54 | options.AddDefaultServer();
|
53 |
| - } |
| 55 | + } |
54 | 56 | }
|
55 | 57 |
|
56 |
| - Servers = new List<DnsEndPoint>(); |
57 |
| - foreach (var server in options.Servers) |
58 |
| - { |
59 |
| - Servers.Add(new DnsEndPoint(server.Address, server.Port)); |
60 |
| - } |
| 58 | + ConfigureServers(options); |
61 | 59 |
|
62 | 60 | SocketPool = new SocketPoolConfiguration();
|
63 | 61 | if (options.SocketPool != null)
|
@@ -172,6 +170,35 @@ public MemcachedClientConfiguration(
|
172 | 170 | }
|
173 | 171 | }
|
174 | 172 |
|
| 173 | + private void ConfigureServers(MemcachedClientOptions options) |
| 174 | + { |
| 175 | + Servers = new List<DnsEndPoint>(); |
| 176 | + foreach (var server in options.Servers) |
| 177 | + { |
| 178 | + if (!IPAddress.TryParse(server.Address, out var address)) |
| 179 | + { |
| 180 | + var ip = Dns.GetHostAddresses(server.Address) |
| 181 | + .FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork)?.ToString(); |
| 182 | + |
| 183 | + if (ip == null) |
| 184 | + { |
| 185 | + _logger.LogError($"Could not resolve host '{server.Address}'."); |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + _logger.LogInformation($"Memcached server address - {server.Address }({ip}):{server.Port}"); |
| 190 | + server.Address = ip; |
| 191 | + } |
| 192 | + } |
| 193 | + else |
| 194 | + { |
| 195 | + _logger.LogInformation($"Memcached server address - {server.Address }:{server.Port}"); |
| 196 | + } |
| 197 | + |
| 198 | + Servers.Add(new DnsEndPoint(server.Address, server.Port)); |
| 199 | + } |
| 200 | + } |
| 201 | + |
175 | 202 | /// <summary>
|
176 | 203 | /// Adds a new server to the pool.
|
177 | 204 | /// </summary>
|
|
0 commit comments