Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump autofac version to 8.0 #322

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


<ItemGroup>
<PackageReference Include="Autofac" Version="[7.0.0, 8.0.0)" />
<PackageReference Include="Autofac" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 12 additions & 2 deletions src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using AspectCore.DynamicProxy;
using AspectCore.DependencyInjection;
using Autofac;
using Autofac.Core;
using Autofac.Core.Lifetime;

namespace AspectCore.Extensions.Autofac
{
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,23 @@
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="6.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="8.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Specification.Tests" Version="8.0.0" />
</ItemGroup>

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

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

<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<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
}
Loading