diff --git a/src/AspectCore.Extensions.Autofac/AspectCore.Extensions.Autofac.csproj b/src/AspectCore.Extensions.Autofac/AspectCore.Extensions.Autofac.csproj index 53826f5f..fe50a318 100644 --- a/src/AspectCore.Extensions.Autofac/AspectCore.Extensions.Autofac.csproj +++ b/src/AspectCore.Extensions.Autofac/AspectCore.Extensions.Autofac.csproj @@ -21,7 +21,7 @@ - + diff --git a/src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs b/src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs index 7dbddfff..453c8ad2 100644 --- a/src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs +++ b/src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs @@ -2,6 +2,8 @@ using AspectCore.DynamicProxy; using AspectCore.DependencyInjection; using Autofac; +using Autofac.Core; +using Autofac.Core.Lifetime; namespace AspectCore.Extensions.Autofac { @@ -34,12 +36,20 @@ public object Resolve(Type serviceType) #if NET8_0_OR_GREATER public object GetKeyedService(Type serviceType, object serviceKey) { - throw new NotImplementedException(); + if (serviceKey is null) + { + return _componentContext.ResolveOptional(serviceType); + } + return _componentContext.ResolveKeyed(serviceKey, serviceType); } public object GetRequiredKeyedService(Type serviceType, object serviceKey) { - throw new NotImplementedException(); + if (serviceKey is null) + { + return _componentContext.Resolve(serviceType); + } + return _componentContext.ResolveKeyed(serviceKey, serviceType); } #endif } diff --git a/tests/AspectCore.Extensions.Autofac.Test/AspectCore.Extensions.Autofac.Test.csproj b/tests/AspectCore.Extensions.Autofac.Test/AspectCore.Extensions.Autofac.Test.csproj index 57fb4bfa..d2e4a61e 100644 --- a/tests/AspectCore.Extensions.Autofac.Test/AspectCore.Extensions.Autofac.Test.csproj +++ b/tests/AspectCore.Extensions.Autofac.Test/AspectCore.Extensions.Autofac.Test.csproj @@ -18,26 +18,23 @@ - - + - - + - - + all diff --git a/tests/AspectCore.Extensions.Autofac.Test/KeyedServiceTests.cs b/tests/AspectCore.Extensions.Autofac.Test/KeyedServiceTests.cs new file mode 100644 index 00000000..8d5da1f2 --- /dev/null +++ b/tests/AspectCore.Extensions.Autofac.Test/KeyedServiceTests.cs @@ -0,0 +1,73 @@ +using System.Threading.Tasks; +using AspectCore.DependencyInjection; +using AspectCore.DynamicProxy; +using AspectCore.Extensions.Autofac; +using AspectCore.Extensions.DependencyInjection; +using Autofac; +using Autofac.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection; +using Xunit; + +namespace AspectCoreTest.Autofac; + +public class KeyedServiceTests +{ + public class InterceptKey : AbstractInterceptorAttribute + { + private int _key; + + public InterceptKey(int key) + { + _key = key; + } + + public override async Task Invoke(AspectContext context, AspectDelegate next) + { + await context.Invoke(next); + context.ReturnValue = _key; + } + } + + public interface IKeydService + { + int Get(); + int GetIntercept(); + } + + public class KeydService : IKeydService + { + private int _current = 0; + public int Get() + { + _current++; + return _current; + } + + [InterceptKey(1000)] + public int GetIntercept() + { + return 2; + } + } +#if NET8_0_OR_GREATER + [Fact] + public void GetKeydService_WithServiceProvider() + { + var services = new ServiceCollection(); + var builder = new ContainerBuilder(); + builder.RegisterDynamicProxy(); + services.AddKeyedScoped("key1"); + services.AddKeyedScoped("key2"); + builder.Populate(services); + var serviceProvider = new AutofacServiceProvider(builder.Build()); + var keydService = serviceProvider.GetKeyedService("key1"); + Assert.Equal(1, keydService.Get()); + Assert.Equal(1000, keydService.GetIntercept()); + + var keyd2Service = serviceProvider.GetKeyedService("key2"); + //不同实例 + Assert.Equal(1, keyd2Service.Get()); + Assert.Equal(1000, keyd2Service.GetIntercept()); + } +#endif +} \ No newline at end of file