2

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.

Mark Simpson
  • 23,245
  • 2
  • 44
  • 44

2 Answers2

2

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
  • 33,668
  • 7
  • 97
  • 131
  • Thanks for the answer, Peter. Since this is for code profiling I'd like to keep it as light as possible, so I guess I'll have to check out the performance of doing the check inside the interceptor & also check its flexibility etc. – Mark Simpson Apr 19 '10 at 10:49
  • 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 Apr 19 '10 at 11:25
  • 4
    I know that this is an old question but for those that are reading this, with newer version of DynamicProxy2, it is now possible to use `.EnableClassInterceptors(new ProxyGenerationOptions(hook))` to specify the hook – Cyril Durand Mar 02 '15 at 14:49
2

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));
CodeAlchemist
  • 490
  • 5
  • 8