views:

199

answers:

1

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.

+1  A: 

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.

Peter Lillevold
Mark Simpson
If you feel up to it you could grab the AutofacContrib.DynamicProxy2 source and add the hook manually. That way you can at least get some comparison using a hook vs filtering in the interceptor.
Peter Lillevold
Yup, I'll give that a go :)
Mark Simpson