Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions Enyim.Caching/Configuration/ConfigurationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void CheckForInterface(Type type, Type interfaceType)
// throw new System.Configuration.ConfigurationErrorsException("The type " + type.AssemblyQualifiedName + " must implement " + interfaceType.AssemblyQualifiedName);
}

public static EndPoint ResolveToEndPoint(string value)
public static DnsEndPoint ResolveToEndPoint(string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("value");
Expand All @@ -86,25 +86,8 @@ public static EndPoint ResolveToEndPoint(string value)
if (!Int32.TryParse(parts[1], out port))
throw new ArgumentException("Cannot parse port: " + parts[1], "value");

return ResolveToEndPoint(parts[0], port);
}

public static EndPoint ResolveToEndPoint(string host, int port)
{
if (String.IsNullOrEmpty(host))
throw new ArgumentNullException("host");

IPAddress address;
// parse as an IP address
if (!IPAddress.TryParse(host, out address))
{
var addresses = Dns.GetHostAddresses(host);
address = addresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
if (address == null)
throw new ArgumentException(String.Format("Could not resolve host '{0}'.", host));
}
return new IPEndPoint(address, port);
}
return new DnsEndPoint(parts[0], port);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface IMemcachedClientConfiguration
/// <summary>
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
/// </summary>
IList<EndPoint> Servers { get; }
IList<DnsEndPoint> Servers { get; }

/// <summary>
/// Gets the configuration of the socket pool.
Expand Down
29 changes: 15 additions & 14 deletions Enyim.Caching/Configuration/MemcachedClientConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,22 @@ public MemcachedClientConfiguration(
var options = optionsAccessor.Value;
if ((options == null || options.Servers.Count == 0) && configuration != null)
{
configuration.GetSection("enyimMemcached").Bind(options);
}

Servers = new List<EndPoint>();
foreach (var server in options.Servers)
{
IPAddress address;
if (IPAddress.TryParse(server.Address, out address))
var section = configuration.GetSection("enyimMemcached");
if (section.Exists())
{
Servers.Add(new IPEndPoint(address, server.Port));
section.Bind(options);
}
else
{
Servers.Add(new DnsEndPoint(server.Address, server.Port));
}
_logger.LogWarning($"No enyimMemcached setting in appsetting.json. Use default configuration");
options.AddDefaultServer();
}
}

Servers = new List<DnsEndPoint>();
foreach (var server in options.Servers)
{
Servers.Add(new DnsEndPoint(server.Address, server.Port));
}

SocketPool = new SocketPoolConfiguration();
Expand Down Expand Up @@ -187,13 +188,13 @@ public void AddServer(string address)
/// <param name="port">The port number of the memcached instance.</param>
public void AddServer(string host, int port)
{
this.Servers.Add(ConfigurationHelper.ResolveToEndPoint(host, port));
this.Servers.Add(new DnsEndPoint(host, port));
}

/// <summary>
/// Gets a list of <see cref="T:IPEndPoint"/> each representing a Memcached server in the pool.
/// </summary>
public IList<EndPoint> Servers { get; private set; }
public IList<DnsEndPoint> Servers { get; private set; }

/// <summary>
/// Gets the configuration of the socket pool.
Expand Down Expand Up @@ -250,7 +251,7 @@ public ITranscoder Transcoder

#region [ interface ]

IList<System.Net.EndPoint> IMemcachedClientConfiguration.Servers
IList<System.Net.DnsEndPoint> IMemcachedClientConfiguration.Servers
{
get { return this.Servers; }
}
Expand Down
2 changes: 2 additions & 0 deletions Enyim.Caching/Configuration/MemcachedClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void AddServer(string address, int port)
Servers.Add(new Server { Address = address, Port = port });
}

public void AddDefaultServer() => AddServer("memcached", 11211);

public void AddPlainTextAuthenticator(string zone, string userName, string password)
{
Authentication = new Authentication
Expand Down
2 changes: 1 addition & 1 deletion Enyim.Caching/Enyim.Caching.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Description>EnyimMemcachedCore is a Memcached client library for .NET Core. Usage: Add services.AddEnyimMemcached(...) and app.UseEnyimMemcached() in Startup. Add IMemcachedClient into constructor.</Description>
<VersionPrefix>2.1.6</VersionPrefix>
<VersionPrefix>2.1.7</VersionPrefix>
<Authors>cnblogs.com</Authors>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand Down
15 changes: 13 additions & 2 deletions Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ public static IServiceCollection AddEnyimMemcached(this IServiceCollection servi
throw new ArgumentNullException(nameof(configurationSection));
}

if(!configurationSection.Exists())
{
throw new ArgumentNullException($"{configurationSection.Key} in appsettings.json");
}

return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(configurationSection));
}

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

return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(configuration.GetSection(sectionKey)));
var section = configuration.GetSection(sectionKey);
if (!section.Exists())
{
throw new ArgumentNullException($"{sectionKey} in appsettings.json");
}

return AddEnyimMemcachedInternal(services, s => s.Configure<MemcachedClientOptions>(section));
}

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

return services;
}
}
}
}
Loading