Skip to content

Commit

Permalink
add autofac KeyedServiceTests.
Browse files Browse the repository at this point in the history
Signed-off-by: nivalxer <[email protected]>
  • Loading branch information
nivalxer committed Mar 27, 2024
1 parent 14183a0 commit 88cf417
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,9 @@
<ProjectReference Include="..\..\src\AspectCore.Extensions.DependencyInjection\AspectCore.Extensions.DependencyInjection.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net7.0' ">
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
Expand Down
73 changes: 73 additions & 0 deletions tests/AspectCore.Extensions.Autofac.Test/KeyedServiceTests.cs
Original file line number Diff line number Diff line change
@@ -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<IKeydService, KeydService>("key1");
services.AddKeyedScoped<IKeydService, KeydService>("key2");
builder.Populate(services);
var serviceProvider = new AutofacServiceProvider(builder.Build());
var keydService = serviceProvider.GetKeyedService<IKeydService>("key1");
Assert.Equal(1, keydService.Get());
Assert.Equal(1000, keydService.GetIntercept());

var keyd2Service = serviceProvider.GetKeyedService<IKeydService>("key2");
//不同实例
Assert.Equal(1, keyd2Service.Get());
Assert.Equal(1000, keyd2Service.GetIntercept());
}
#endif
}

0 comments on commit 88cf417

Please sign in to comment.