diff --git a/Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs b/Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs index 6727d5f3..bb197b2e 100644 --- a/Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs +++ b/Enyim.Caching/EnyimMemcachedServiceCollectionExtensions.cs @@ -46,27 +46,11 @@ private static IServiceCollection AddEnyimMemcached(IServiceCollection services, { services.AddOptions(); configure(services); - services.Add(ServiceDescriptor.Transient()); - services.Add(ServiceDescriptor.Singleton()); - return services; - } - - public static IServiceCollection AddDistributedEnyimMemcached(this IServiceCollection services, Action setupAction) - { - if (services == null) - { - throw new ArgumentNullException(nameof(services)); - } + services.AddTransient(); + services.AddSingleton(); - if (setupAction == null) - { - throw new ArgumentNullException(nameof(setupAction)); - } - - services.AddOptions(); - services.Configure(setupAction); - services.Add(ServiceDescriptor.Transient()); - services.Add(ServiceDescriptor.Singleton()); + services.AddSingleton(factory => factory.GetService()); + services.AddSingleton(factory => factory.GetService()); return services; } diff --git a/SampleWebApp/Program.cs b/SampleWebApp/Program.cs index 90067053..88238ae6 100644 --- a/SampleWebApp/Program.cs +++ b/SampleWebApp/Program.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore; namespace SampleWebApp { @@ -11,13 +12,14 @@ public class Program { public static void Main(string[] args) { - var host = new WebHostBuilder() - .UseKestrel() - .UseContentRoot(Directory.GetCurrentDirectory()) + BuildWebHost(args).Run(); + } + + public static IWebHost BuildWebHost(string[] args) + { + return WebHost.CreateDefaultBuilder(args) .UseStartup() .Build(); - - host.Run(); } } } diff --git a/SampleWebApp/SampleWebApp.csproj b/SampleWebApp/SampleWebApp.csproj index a656af0b..b1352455 100644 --- a/SampleWebApp/SampleWebApp.csproj +++ b/SampleWebApp/SampleWebApp.csproj @@ -13,4 +13,10 @@ + + + PreserveNewest + + + diff --git a/SampleWebApp/Startup.cs b/SampleWebApp/Startup.cs index ff20ff39..96202fc9 100644 --- a/SampleWebApp/Startup.cs +++ b/SampleWebApp/Startup.cs @@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging; using Enyim.Caching; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Caching.Distributed; namespace SampleWebApp { @@ -36,22 +37,26 @@ public void ConfigureServices(IServiceCollection services) public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { - loggerFactory.AddConsole(LogLevel.Debug); - app.UseEnyimMemcached(); var memcachedClient = app.ApplicationServices.GetService(); + var distributedCache = app.ApplicationServices.GetService(); var logger = loggerFactory.CreateLogger(); app.Run(async (context) => { var cacheKey = "sample_response"; - await memcachedClient.AddAsync(cacheKey, "Hello World!", 60); + var distributedCaceKey = "distributed_" + cacheKey; + await memcachedClient.AddAsync(cacheKey, $"Hello World from {nameof(memcachedClient)}!", 60); + await distributedCache.SetStringAsync(distributedCaceKey,$"Hello World from {nameof(distributedCache)}!"); var cacheResult = await memcachedClient.GetAsync(cacheKey); if (cacheResult.Success) { - await context.Response.WriteAsync(cacheResult.Value); + var distributedCacheValue = await distributedCache.GetStringAsync(distributedCaceKey); + await context.Response + .WriteAsync($"memcachedClient: {cacheResult.Value}\ndistributedCache: {distributedCacheValue}"); await memcachedClient.RemoveAsync(cacheKey); + await distributedCache.RemoveAsync(distributedCaceKey); logger.LogDebug($"Hinted cache with '{cacheKey}' key"); } else diff --git a/SampleWebApp/appsettings.json b/SampleWebApp/appsettings.json index e19a0cde..17352821 100644 --- a/SampleWebApp/appsettings.json +++ b/SampleWebApp/appsettings.json @@ -25,6 +25,14 @@ // "password": "password" // } //} + }, + "Logging": { + "IncludeScopes": false, + "LogLevel": { + "Default": "Debug", + "System": "Warning", + "Microsoft": "Warning" + } } } \ No newline at end of file