Unity Policy Injection for all classes

Dec 19, 2013 at 9:45 PM
      <extension type="Interception"/>
      <register type="IService" mapTo="ApplicationService">
        __<interceptor type="TransparentProxyInterceptor" />

      <register type="IAnotherService" mapTo="OtherApplicationService">

        <policy name="policy-withdraw">
          <matchingRule name="withdraw-rule1" type="TypeMatchingRule"/>
          <callHandler name="withdraw-handler1" type="InterceptionHOL.AccessCheckCallHandler, InterceptionHOL"/>
When i register a type I put the interception policyInjection and type elements. Is there a way to do this for all registered types in my config file? The ApplicationService will get the policyInjection but OtherApplicationService will not, do i have to go through and add the interceptor element and the policyInjection to each registered type?
Dec 26, 2013 at 5:04 PM
There is no out of the box way to "globally" apply interception and/or policy injection using the configuration file.

Probably the easiest way to achieve what you want so to move the configuration to code and then use Registration by Convention (assuming you are using Unity 3).

Here is a slightly modified example fro the Developer's Guide:
var container = new UnityContainer();

      t => t.Namespace == "InterceptionHOL"),
    getInjectionMembers: t => new InjectionMember[]
      new Interceptor<TransparentProxyInterceptor>(),
      new InterceptionBehavior<PolicyInjectionBehavior>()
You can still store the policies in configuration if you wish (just call container.LoadConfiguration())

Other approaches (such as for Unity 2) involve using a Container Extension to add the interceptor and behavior registration. See this question for a short starting sample of this approach: http://unity.codeplex.com/discussions/281022 .

Randy Levy
Enterprise Library support engineer
Support How-to