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

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

Closed
changemyminds opened this issue Sep 2, 2022 · 0 comments
Closed

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

changemyminds opened this issue Sep 2, 2022 · 0 comments
Assignees
Labels

Comments

@changemyminds
Copy link

changemyminds commented Sep 2, 2022

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 ?

@liuhaoyang liuhaoyang self-assigned this Oct 20, 2022
@liuhaoyang liuhaoyang added the bug label Oct 20, 2022
nivalxer added a commit to nivalxer/AspectCore-Framework that referenced this issue Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants