Unity interception and configuration

Jun 10, 2011 at 11:56 AM

Hello everyone,

 I am using Unity to test something before it will be included in my project and I was able to configure interception programmatically without any problems. Basically I have just one interface (ITest) and one class that implements it (Test). I also created a custom handler that is executed for all methods marked with a custom attribute. Then in the code I just do the following:

IUnityContainer container = new UnityContainer().AddNewExtension<Interception>();
Interception>().SetInterceptorFor<ITest>(new TransparentProxyInterceptor());
 test = container.Resolve<ITest>(); 

Whenever I execute any method marked with my custom attribute (implemented in class Test) it works as intended. However, I was not able to define the same setup in the configuration file. The official documentation is also not that helpful in that case. Perhaps someone has an idea how to configure the same behavior in the configuration file, so that I will not have to configure interception in the code?

Kind regards,

Jun 13, 2011 at 10:24 AM


What particular challenge did you encountered? Though I haven't tested this yet, have you tried:

    <section name="unity"
             Microsoft.Practices.Unity.Configuration, Version=2.0.414.0,                 
             Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type=
     Microsoft.Practices.Unity.Interception.Configuration"  />
      <extension type="Interception"/>
      <register type="ITest" mapTo="Test">
        <interceptor type="TransparentProxyInterceptor"/>

Note that you should specify the partial or fully qualified names of type ITest and Test. For more details you can refer here

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.

Jun 13, 2011 at 2:18 PM
Edited Jun 13, 2011 at 2:19 PM


It looks like an extremely easy task, but somehow it does not work. I have exactly the same code as you provided, but with one difference. All my configuration is stored in the App.Config file and the Unity section is stored in the Unity.config.

Here is the App.Config:


<?xml version="1.0" encoding="utf-8" ?>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, 
/> </configSections> <unity configSource="Unity.config"/> </configuration>

Here is the Unity.config:


<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
  <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,  
/> <alias alias="ITest" type="TestApplication.ITest, TestApplication"/> <alias alias="test" type="TestApplication.Test, TestApplication"/> <container> <extension type="Interception"/> <register type="ITest" mapTo="test"> <interceptor type="TransparentProxyInterceptor" /> </register> </container> </unity>


Now the funny part - I am able to use container.Resolve<ITest>() in the code and it works just fine, but instead of returning a transparent proxy it always returns the instance of TestApplication.Test. That means that the "register" section worked correctly, but somehow the transparent proxy is never created in that case. Do you have any idea of what might be wrong?

Kind regards,

Jun 13, 2011 at 5:58 PM
You need to explicitly enable the policy injection behavior in the configuration file. You're going through the old registration API in the code - that one automatically enables policy injection for backwards compatibility reasons.

Change your register tag to this:

    <register type="ITest" mapTo="test">
      <interceptor type="TransparentProxyInterceptor" />
      <policyInjection />

and you should be all set. By the way, the new API for this looks like this:

container.RegisterType<ITest, Test>(
    new Interceptor<TransparentProxyInterceptor>(),
    new InterceptionBehavior<PolicyInjectionBehavior>());


Jun 14, 2011 at 8:09 AM

That is what I have been looking for! Works like a charm :) Thank you very much for help.

Best regards,