dotnetcore / AspectCore-Framework

AspectCore is an AOP-based cross platform framework for .NET Standard.
MIT License
1.69k stars 324 forks source link

Generics interface inherit bugs in ASP.NET Core 6 #289

Closed changemyminds closed 1 year ago

changemyminds commented 2 years ago

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.

public interface IGenericService { T TestGeneric1();

void TestGeneric2();

}

public interface IAService { void TestA(); }

public interface IBService : IAService, IGenericService { 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
```csharp
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);
    }
}

var builder = WebApplication.CreateBuilder(args); builder.Services.AddSingleton<IBService,TestService>(); builder.Services.AddSingleton(); builder.Services.ConfigureDynamicProxy(config => { config.Interceptors.AddServiced( Predicates.ForService("*Service") ); }); builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());

var app = builder.Build(); using var scop = app.Services.CreateScope(); var bService = scop.ServiceProvider.GetRequiredService(); 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](https://github.com/changemyminds/Reproduce.AspectCore_Framework.Bugs/tree/use/Castle.Core). But I like 
`AspectCore-Framework` library anybody can help ?