AspectCore-Framework icon indicating copy to clipboard operation
AspectCore-Framework copied to clipboard

Interceptor do not take effect when using delegation to create instances

Open catcherwong opened this issue 5 years ago • 1 comments

I have an intercept named TestIntercept and it implemented AbstractInterceptorAttribute.

    public class TestIntercept : AbstractInterceptorAttribute
    {
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            Console.WriteLine("Interceptor effect");
            return context.Invoke(next);
        }
    }

And have a test service to use it.

 public interface ITestService
    {
        [TestIntercept]
        string GetCurrentUtcTime();
    }

    public class TestService : ITestService
    {      
        public string GetCurrentUtcTime()
        {
            return DateTimeOffset.UtcNow.ToString();
        }
    }

The issue I met is as follow.

When using services.AddScoped<ITestService, TestService>(), the interceptor runs well.

But using services.AddScoped<ITestService>(x => new TestService());, the interceptor can not run well.

Here is the sample code I used to configure.

public void ConfigureServices(IServiceCollection services)
{
    // 1. this one can run well
    //services.AddScoped<ITestService, TestService>();

    // 2. this one can not Intercept
    services.AddScoped<ITestService>(x => new TestService());

    services.ConfigureDynamicProxy(config =>
    {
        config.Interceptors.AddTyped<TestIntercept>();
    });

    services.AddControllers();
}

Does I miss somethings for this issue?

catcherwong avatar Oct 24 '20 16:10 catcherwong

Hi, I met this issue as well...

@catcherwong Could you please tell me how do you resolve this issue? Thank you very much.

LeBron666 avatar Jan 25 '22 07:01 LeBron666