From 74615ceaa1a6106e8e73e187993504db59fa88ea Mon Sep 17 00:00:00 2001 From: Alastair Crabtree Date: Sat, 28 Dec 2019 14:25:29 +0000 Subject: [PATCH] task: improve constructor resolution in aspnet core dependency injection --- .../LazyCacheServiceCollectionExtensions.cs | 4 +- .../LazyCache.Ninject.UnitTests.csproj | 8 +--- LazyCache.UnitTests/AspNetCoreTests.cs | 42 +++++++++++++++++++ .../LazyCache.UnitTests.csproj | 2 + 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 LazyCache.UnitTests/AspNetCoreTests.cs diff --git a/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs b/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs index 8739b3f..81df994 100644 --- a/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs +++ b/LazyCache.AspNetCore/LazyCacheServiceCollectionExtensions.cs @@ -18,7 +18,9 @@ public static IServiceCollection AddLazyCache(this IServiceCollection services) services.TryAdd(ServiceDescriptor.Singleton()); services.TryAdd(ServiceDescriptor.Singleton()); - services.TryAdd(ServiceDescriptor.Singleton()); + services.TryAdd(ServiceDescriptor.Singleton(serviceProvider => + new CachingService( + new Lazy(serviceProvider.GetRequiredService)))); return services; } diff --git a/LazyCache.Ninject.UnitTests/LazyCache.Ninject.UnitTests.csproj b/LazyCache.Ninject.UnitTests/LazyCache.Ninject.UnitTests.csproj index 6426959..b7037ea 100644 --- a/LazyCache.Ninject.UnitTests/LazyCache.Ninject.UnitTests.csproj +++ b/LazyCache.Ninject.UnitTests/LazyCache.Ninject.UnitTests.csproj @@ -1,7 +1,7 @@ - netcoreapp2.2 + netcoreapp3.1 false @@ -16,10 +16,4 @@ - - - C:\Users\Alastair\.nuget\packages\ninject\3.3.4\lib\netstandard2.0\Ninject.dll - - - diff --git a/LazyCache.UnitTests/AspNetCoreTests.cs b/LazyCache.UnitTests/AspNetCoreTests.cs new file mode 100644 index 0000000..deabd8c --- /dev/null +++ b/LazyCache.UnitTests/AspNetCoreTests.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using FluentAssertions; +using Microsoft.Extensions.DependencyInjection; +using NUnit.Framework; + +namespace LazyCache.UnitTests +{ + public class AspNetCoreTests + { + [Test] + public void CanResolveCacheFromServiceCollectionAsRequiredService() + { + var container = new ServiceCollection(); + container.AddLazyCache(); + var provider = container.BuildServiceProvider(); + + var cache = provider.GetRequiredService(); + var result = cache?.GetOrAdd("key", () => new object()); + + cache.Should().NotBeNull(); + result.Should().NotBeNull(); + } + + [Test] + public void CanResolveCacheFromServiceCollectionAsService() + { + var container = new ServiceCollection(); + container.AddLazyCache(); + var provider = container.BuildServiceProvider(); + + var cache = provider.GetService(); + var result = cache?.GetOrAdd("key", () => new object()); + + cache.Should().NotBeNull(); + result.Should().NotBeNull(); + } + } +} diff --git a/LazyCache.UnitTests/LazyCache.UnitTests.csproj b/LazyCache.UnitTests/LazyCache.UnitTests.csproj index b4eb31e..a7d9b4e 100644 --- a/LazyCache.UnitTests/LazyCache.UnitTests.csproj +++ b/LazyCache.UnitTests/LazyCache.UnitTests.csproj @@ -8,12 +8,14 @@ + +