How to disable unity interface interception (AOP)

Topics: Enterprise Library Core, General discussion
Oct 10, 2012 at 3:38 AM

Hello,

I have used the unity interception to make a CallHandler for each method expose via an interface. This interceptor manage the database transaction, exception handling, etc.

However, in some scenario, i would like to disable this interceptor while still using the same or a child of the unity container. There doesn't seem a way to disable or remove the interception once it's configure in the container.

localContainer.Configure<Interception>().SetDefaultInterceptorFor...

I though that creating a child container would bypass the configure section but it doesn't seem to do so.

So aside creating a brand new container, is there way to remove an already configure interception patter on a unity container.

Thank you

Oct 10, 2012 at 7:48 AM
Edited Oct 10, 2012 at 6:50 PM

It doesn't look like you can entirely remove the interception. (At least, it doesn't look particularly easy -- perhaps a UnityContainerExtension could be written to remove the interception.)

One approach would be to use Policy Injection.

So you could create an Enable/Disable matching rule:

    public class EnabledMatchingRule : IMatchingRule
    {
        private bool isEnabled;

        public EnabledMatchingRule(bool isEnabled)
        {
            this.isEnabled = isEnabled;
        }

        public bool Matches(System.Reflection.MethodBase member)
        {
            return isEnabled;
        }
    }

Then you could setup the policy with enabled set to true and also register interception.  So here I add the interception extension, register my call handler, LoggingCallBehavior, configure the policy with the matching rule enabled and then setup interception on my type:

    container.AddNewExtension<Interception>();

    container.RegisterType<LoggingCallBehavior>(new ContainerControlledLifetimeManager(), new InjectionConstructor());

    container.Configure<Interception>()
        .AddPolicy("policy")
            .AddMatchingRule<EnabledMatchingRule>(new ContainerControlledLifetimeManager(), new InjectionConstructor(true))
            .AddCallHandler(container.Resolve<LoggingCallBehavior>());

    container.RegisterType<SampleDomainService>(
        new Interceptor<VirtualMethodInterceptor>(),
        new InterceptionBehavior<PolicyInjectionBehavior>());

If later I want to disable interception then you can replace the policy and set the enabled InjectionConstructor to false:

    container.Configure<Interception>()
        .AddPolicy("policy")
            .AddMatchingRule<EnabledMatchingRule>(new ContainerControlledLifetimeManager(), new InjectionConstructor(false))
            .AddCallHandler(container.Resolve<LoggingCallBehavior>());

The good part is if the matching rule returns false then interception is not enabled and the actual type (and not a proxy etc.) is returned.

Hopefully, that fits with your design.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Oct 10, 2012 at 6:06 PM

Hello,

This looks like the proper solution for my design :-)

Thank you