autofac / Autofac.Extras.DynamicProxy

Interceptor and decorator support for Autofac IoC via Castle DynamicProxy
MIT License
106 stars 33 forks source link

Autofac Not Intercepting #28

Closed tillig closed 6 years ago

tillig commented 6 years ago

From @CShelton11 on June 5, 2018 21:25

I cannot get autofac to intercept. I have the following setup in my .net core application:

// **** // Logger created: // ****

public class Logger : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        // Logging removed for now
        var watch = System.Diagnostics.Stopwatch.StartNew();  // Added break point here
        invocation.Proceed(); 
        watch.Stop();
        var executionTime = watch.ElapsedMilliseconds;
    }
}

// **** // Class Created: // ****

[Intercept(typeof(Logger))]
public class ServiceProxy: ServiceInterface
{
    public User GetUser(String username, String password)
    {
        var service = ServiceHelper.GetODataClaimService();
        var query = from a in service.Users
                            select a;
        var dsq = query.ToDataServiceQuery<User>();
        var result = dsq.ToListSync<User>();
        var user = result.FirstOrDefault();
        return user;
    }
}

// **** // Interface Created: // ****

public interface ServiceInterface
{
    User GetUser(String username, String password);
}

// **** // Interception Configuration // ****

public class Interceptor
{
    public static void Configure()
    {
        var builder = new ContainerBuilder();
        builder.Register(a => new Logger());
        builder.RegisterType<ServiceProxy>().As<ServiceInterface>().EnableInterfaceInterceptors().InterceptedBy(typeof(Logger));  // Tried removing intercepted by
        var container = builder.Build();
        var worker = container.Resolve<ServiceInterface>();
        builder.Build()
    }
}

I put a break point in the logger to see if it ever enters that block of code. It never does. What am I missing here? I've tried quite a few configurations but nothing seems to work. Also - The Configure method is invoked from application Startup..

Please advise.

Copied from original issue: autofac/Documentation#56

tillig commented 6 years ago

Have you asked this on StackOverflow? We recommend asking usage (how do I...? others have this working but I need help..,.?) sorts of questions there and tagging it autofac. We monitor there, but so do many others. You will likely get an answer faster there.

If you do, please post a link to the question here and then close this issue.

CShelton11 commented 6 years ago

Move to stack overflow: https://stackoverflow.com/questions/50709918/autofac-interception-not-working