Question

I'm using Castle DynamicProxy with Autofac. I have an object for which I've created a proxy, and I have two interceptors that act on the proxy, one for logging an exception and the second for for altering the return value of the method. The registration code looks like the following:

var builder = new ContainerBuilder();

builder.Register(c => c.Resolve<ProxyGenerator>()
    .CreateClassProxy<Foo>(
        c.Resolve<ResultProcessorInterceptor>(),
        c.Resolve<ExceptionLoggingInterceptor>()))
    .As<Foo>();

By supplying the arguments in this order, I'm finding that I get the result that I want, i.e., the exception is logged and the result is processed. If I reverse the order of the arguments, the logging doesn't occur.

My question, then: registered in this way, are the interceptors guaranteed to execute in the same order every time? Or is there a better way to ensure that the order will be what I intend every time?

FWIW, I looked at the IInterceptorSelector interface. Perhaps I'm missing something--which is not unlikely--but it looked like that wasn't relevant in this case. But I'd be happy to be corrected if I'm wrong.

I can supply a longer code sample if necessary.

musicologyman

Was it helpful?

Solution

Yes, the interceptors will be by default executed in the provided order for every intercepted method.

As you correctly noted you can override it on a per-method basis using IInterceptorSelector if needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top