Selectively intercepting methods using autofac and dynamicproxy2

Posted by Mark Simpson on Stack Overflow See other posts from Stack Overflow or by Mark Simpson
Published on 2010-04-18T16:31:55Z Indexed on 2010/04/18 16:33 UTC
Read the original article Hit count: 459

Filed under:
|
|

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 gather that 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 say "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.

© Stack Overflow or respective owner

Related posts about c#

Related posts about autofac