Question

I'm currently doing a bit of experimenting using Autofac-1.4.5.676, autofac contrib and castle DynamicProxy2. The goal is to create a coarse-grained profiler that can intercept calls to specific methods of a particular interface.

The problem: I have everything working perfectly apart from the selective part. I could be wrong, but I think I need to marry up my interceptor with an IProxyGenerationHook implementation, but I can't figure out how to do this.

My code looks something like this:

The interface that is to be intercepted & profiled (note that I only care about profiling the Update() method)

public interface ISomeSystemToMonitor
{
    void Update(); // this is the one I want to profile
    void SomeOtherMethodWeDontCareAboutProfiling();
}

Now, when I register my systems with the container, I do the following:

// Register interceptor gubbins
builder.RegisterModule(new FlexibleInterceptionModule());
builder.Register<PerformanceInterceptor>();

// Register systems (just one in this example)
builder.Register<AudioSystem>()
.As<ISomeSystemToMonitor>)
.InterceptedBy(typeof(PerformanceInterceptor)); 

All ISomeSystemToMonitor instances pulled out of the container are intercepted and profiled as desired, other than the fact that it will intercept all of its methods, not just the Update method.

Now, how can I extend this to exclude all methods other than Update()? As I said, I don't understand how I'm meant to inform the container that, "for the ProfileInterceptor, use this implementation of IProxyHookGenerator".

All help appreciated, cheers! Also, please note that I can't upgrade to autofac2.x right now; I'm stuck with 1.

Was it helpful?

Solution

A IProxyGenerationHook instance have to be passed to the CreateInterfaceProxyWithTarget call when the interceptor is produced. See this tutorial for some more details.

Currently there doesn't seem to be a way of providing such hooks without changes to the Autofac.DynamicProxy2 integration module. Could be a nice addition to the InterceptedBy extension.

Alternatively you could build the filtering into PerformanceInterceptor. Looking at the IInvocation you're passed on invocation, examine the Method property to decide whether to profile or not. But this will of course be less efficient than bypassing interception at the proxy level.

OTHER TIPS

For DynamicProxy2 , the EnableInterfaceInterceptors method now has an overload that takes a ProxyGenerationOptions object.

//Define the builder
var builder = new ContainerBuilder();

//Instantiate your Proxy options with a selector
var proxyOptions = new ProxyGenerationOptions {Selector = new MyInterceptSelector()};

//Pass the proxy options as a parameter to the EnableInterfaceInterceptors method
builder.RegisterType<MyRepo>()
            .As<IMyRepo>()
            .EnableInterfaceInterceptors(proxyOptions)
            .InterceptedBy(typeof(IInterceptor));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top