Skip to content

Commit 438508b

Browse files
committed
add autofac KeyedServiceTests.
Signed-off-by: nivalxer <nivalxer@gmail.com>
1 parent 4d374f6 commit 438508b

File tree

2 files changed

+74
-14
lines changed

2 files changed

+74
-14
lines changed

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,9 @@
1717
<ProjectReference Include="..\..\src\AspectCore.Extensions.DependencyInjection\AspectCore.Extensions.DependencyInjection.csproj" />
1818
</ItemGroup>
1919

20-
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
21-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
22-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
23-
</ItemGroup>
24-
25-
<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
26-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
27-
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
28-
</ItemGroup>
29-
30-
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
20+
<ItemGroup>
3121
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
3222
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
33-
</ItemGroup>
34-
35-
<ItemGroup>
3623
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
3724
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
3825
<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)