Binding Interception Policy to named registration

Feb 14, 2011 at 6:56 PM


I have the following registration/policy



unityContainer.Configure<Interception>().SetInterceptorFor<TestObject2>("named",new VirtualMethodInterceptor()). AddPolicy("theNameIsNotImportant") .AddMatchingRule<TypeMatchingRule>(new InjectionConstructor("Anbahnung.Tests.Aspects.TestObject2")) .AddCallHandler<ExceptionCallHandler>(new InjectionProperty("PolicyName", "TestAspectExceptionHandler"));



Now I have another registration of the same type

                "SomeOtherRegistrationWithInterception", new TransparentProxyInterceptor());

But here I do NOT want the above policy to apply but unfortunately it does....

How can I turn off this behavior or in other words how can I bind a policy to a certain registration?

Feb 16, 2011 at 6:16 AM

The behavior you're seeing is expected. Matching rules execute based on the type being injected. The registration name doesn't enter into it. There currently isn't any way to limit policy injection by registration name.

You'll need to either change the type somehow (derived classes?) or decide some other policy.

Another option would be to write an interception behavior instead of using the policy injection call handlers.

Feb 16, 2011 at 1:33 PM

The way unity is configured I would expect it to work. So instead of AddPolicy("SomePointlessName") I would expect to be able to specify the registration. Then I could do

AddPolicy() for the default registration and AddPolicy("MySpecificRegistration") if I want it to apply a policy for a named registration only.

Feb 18, 2011 at 1:33 AM

The name of the policy won't matter either, it won't limit the attaching of the call handlers to your other registration of the same type.  The matching rule(s) will be used to evaluate which type/method will get intercepted. 


Sarah Urmeneta
Global Technologies and Solutions
Avanade, Inc.