AspectCore-Framework
AspectCore-Framework copied to clipboard
Generics interface inherit bugs in ASP.NET Core 6
I have an interface need inherit other interfaces. But when I inherit the generics interface and call it method the AbstractInterceptorAttribute
isn't work fine.
Here is reproduce example.
- DemoInterface.cs
public class Sample
{
}
public interface IGenericService<T>
{
T TestGeneric1();
void TestGeneric2();
}
public interface IAService
{
void TestA();
}
public interface IBService : IAService, IGenericService<Sample>
{
void TestB();
}
public class TestService : IBService
{
public void TestA()
{
System.Console.WriteLine("Call TestA");
}
public void TestB()
{
System.Console.WriteLine("Call TestB");
}
public Sample TestGeneric1()
{
System.Console.WriteLine("Call TestGeneric1");
return new Sample();
}
public void TestGeneric2()
{
System.Console.WriteLine("Call TestGeneric2");
}
}
- TestAopAttribute.cs
public class TestAopAttribute : AbstractInterceptorAttribute
{
public override async Task Invoke(AspectContext context, AspectDelegate next)
{
var className = context.Implementation.GetType().FullName;
var methodName = context.ServiceMethod.Name;
System.Console.WriteLine($"{className}, {methodName}");
await next(context);
}
}
- Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IBService,TestService>();
builder.Services.AddSingleton<TestAopAttribute>();
builder.Services.ConfigureDynamicProxy(config =>
{
config.Interceptors.AddServiced<TestAopAttribute>(
Predicates.ForService("*Service")
);
});
builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
var app = builder.Build();
using var scop = app.Services.CreateScope();
var bService = scop.ServiceProvider.GetRequiredService<IBService>();
bService.TestA();
bService.TestB();
bService.TestGeneric1();
bService.TestGeneric2();
I expect console print result.
AspectCore_Framework.Bugs.TestService, TestA
Call TestA
AspectCore_Framework.Bugs.TestService, TestB
Call TestB
AspectCore_Framework.Bugs.TestService, TestGeneric1
Call TestGeneric1
AspectCore_Framework.Bugs.TestService, TestGeneric2
Call TestGeneric2
But actually console print result.
AspectCore_Framework.Bugs.TestService, TestA
Call TestA
AspectCore_Framework.Bugs.TestService, TestB
Call TestB
Call TestGeneric1
Call TestGeneric2
If I use this case in Castle.Core
library everything work fine see demo project. But I like
AspectCore-Framework
library anybody can help ?