Skip to content

Commit 8814c21

Browse files
committed
Bump autofac version to 8.0
add autofac KeyedServiceTests. Signed-off-by: nivalxer <nivalxer@gmail.com>
1 parent a7d1dea commit 8814c21

File tree

4 files changed

+89
-9
lines changed

4 files changed

+89
-9
lines changed

src/AspectCore.Extensions.Autofac/AspectCore.Extensions.Autofac.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222

2323
<ItemGroup>
24-
<PackageReference Include="Autofac" Version="[7.0.0, 8.0.0)" />
24+
<PackageReference Include="Autofac" Version="8.0.0" />
2525
</ItemGroup>
2626

2727
<ItemGroup>

src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using AspectCore.DynamicProxy;
33
using AspectCore.DependencyInjection;
44
using Autofac;
5+
using Autofac.Core;
6+
using Autofac.Core.Lifetime;
57

68
namespace AspectCore.Extensions.Autofac
79
{
@@ -34,12 +36,20 @@ public object Resolve(Type serviceType)
3436
#if NET8_0_OR_GREATER
3537
public object GetKeyedService(Type serviceType, object serviceKey)
3638
{
37-
throw new NotImplementedException();
39+
if (serviceKey is null)
40+
{
41+
return _componentContext.ResolveOptional(serviceType);
42+
}
43+
return _componentContext.ResolveKeyed(serviceKey, serviceType);
3844
}
3945

4046
public object GetRequiredKeyedService(Type serviceType, object serviceKey)
4147
{
42-
throw new NotImplementedException();
48+
if (serviceKey is null)
49+
{
50+
return _componentContext.Resolve(serviceType);
51+
}
52+
return _componentContext.ResolveKeyed(serviceKey, serviceType);
4353
}
4454
#endif
4555
}

tests/AspectCore.Extensions.Autofac.Test/AspectCore.Extensions.Autofac.Test.csproj

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,23 @@
1818
</ItemGroup>
1919

2020
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
21-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
22-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="6.0.0" />
21+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
2322
</ItemGroup>
2423

2524
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
26-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
27-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="7.0.0" />
25+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
2826
</ItemGroup>
2927

3028
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
31-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
3229
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
3330
</ItemGroup>
3431

3532
<ItemGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
36-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
3733
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="9.0.0" />
3834
</ItemGroup>
3935

4036
<ItemGroup>
37+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
4138
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
4239
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
4340
<PrivateAssets>all</PrivateAssets>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System.Threading.Tasks;
2+
using AspectCore.DependencyInjection;
3+
using AspectCore.DynamicProxy;
4+
using AspectCore.Extensions.Autofac;
5+
using AspectCore.Extensions.DependencyInjection;
6+
using Autofac;
7+
using Autofac.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Xunit;
10+
11+
namespace AspectCoreTest.Autofac;
12+
13+
public class KeyedServiceTests
14+
{
15+
public class InterceptKey : AbstractInterceptorAttribute
16+
{
17+
private int _key;
18+
19+
public InterceptKey(int key)
20+
{
21+
_key = key;
22+
}
23+
24+
public override async Task Invoke(AspectContext context, AspectDelegate next)
25+
{
26+
await context.Invoke(next);
27+
context.ReturnValue = _key;
28+
}
29+
}
30+
31+
public interface IKeydService
32+
{
33+
int Get();
34+
int GetIntercept();
35+
}
36+
37+
public class KeydService : IKeydService
38+
{
39+
private int _current = 0;
40+
public int Get()
41+
{
42+
_current++;
43+
return _current;
44+
}
45+
46+
[InterceptKey(1000)]
47+
public int GetIntercept()
48+
{
49+
return 2;
50+
}
51+
}
52+
#if NET8_0_OR_GREATER
53+
[Fact]
54+
public void GetKeydService_WithServiceProvider()
55+
{
56+
var services = new ServiceCollection();
57+
var builder = new ContainerBuilder();
58+
builder.RegisterDynamicProxy();
59+
services.AddKeyedScoped<IKeydService, KeydService>("key1");
60+
services.AddKeyedScoped<IKeydService, KeydService>("key2");
61+
builder.Populate(services);
62+
var serviceProvider = new AutofacServiceProvider(builder.Build());
63+
var keydService = serviceProvider.GetKeyedService<IKeydService>("key1");
64+
Assert.Equal(1, keydService.Get());
65+
Assert.Equal(1000, keydService.GetIntercept());
66+
67+
var keyd2Service = serviceProvider.GetKeyedService<IKeydService>("key2");
68+
//不同实例
69+
Assert.Equal(1, keyd2Service.Get());
70+
Assert.Equal(1000, keyd2Service.GetIntercept());
71+
}
72+
#endif
73+
}

0 commit comments

Comments
 (0)