I can't use Interception to my Application

Aug 13, 2014 at 1:48 AM
Edited Aug 13, 2014 at 1:50 AM
Hi

I try to use Unity's Interception

I set DI container in my configuration file
 <unity>
    <namespace name="UnityTest.App_Code" />
    <assembly name="UnityTest" />
    <container name="TenantContainer">
      <register type="ITenantStore" mapTo="TenantStore" />
    </container>
  </unity>
and write some register code
 var container = new UnityContainer();
 container.LoadConfiguration("TenantContainer");
 container.AddNewExtension<Interception>();
 container.RegisterType<ITenantStore>(
                                new InterceptionBehavior<PolicyInjectionBehavior>(),
                                new Interceptor<InterfaceInterceptor>());

return container.Resolve<T>();
It's work. but when I add name in register type of configuration file like that
<unity>
    <namespace name="UnityTest.App_Code" />
    <assembly name="UnityTest" />
    <container name="TenantContainer">
       <register type="ITenantStore" mapTo="TenantStore" name="First"  />
    </container>
  </unity>
 var container = new UnityContainer();
 container.LoadConfiguration("TenantContainer");
 container.AddNewExtension<Interception>();
 container.RegisterType<ITenantStore>(
                                new InterceptionBehavior<PolicyInjectionBehavior>(),
                                new Interceptor<InterfaceInterceptor>());
return container.Resolve<T>("First");
The interception can't work !!

Can anyone help me?

Thks !
Aug 14, 2014 at 6:10 AM
In the first scenario you are registering a default (unnamed) type mapping in the configuration. Then in code you are loading the interception extension and adding injection members to that registration with the interception details.

The reason it's not working in the second case is that in the XML configuration you have used a named registration ("First") but when performing the registration of the injection members in the code you are using a default registration. Unity uses the From Type and Name as the build key when performing a resolve (for example). If you look at container.Registrations you will see that there are now 2 registrations for ITenantStore.

The easiest way to get your example code to work is to add "First" to the RegisterType call:
container.RegisterType<ITenantStore>("First",
                                new InterceptionBehavior<PolicyInjectionBehavior>(),
                                new Interceptor<InterfaceInterceptor>());

Is there any particular reason you are mixing XML and programmatic configuration? Unless there is a compelling reason to mix the two I would recommend sticking with one consistent approach. You could always wire interception up in the XML configuration:
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="UnityTest.App_Code" />
    <assembly name="UnityTest" />
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <container name="TenantContainer">
      <extension type="Interception" />
      <register type="ITenantStore" mapTo="TenantStore" name="First">
        <interceptor type="InterfaceInterceptor" />
        <interceptionBehavior type="LoggingCallBehavior"/>
      </register>
    </container>
  </unity>

var container = new UnityContainer();
container.LoadConfiguration("TenantContainer");

var t = container.Resolve<ITenantStore>("First");

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to