Unity Policy Injection for all classes

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

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

      <interception>
        <policy name="policy-withdraw">
          <matchingRule name="withdraw-rule1" type="TypeMatchingRule"/>
          <callHandler name="withdraw-handler1" type="InterceptionHOL.AccessCheckCallHandler, InterceptionHOL"/>
        </policy>
      </interception>
    </container>
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?
Editor
Dec 26, 2013 at 4: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();

container.AddNewExtension<Interception>();
container.RegisterTypes(
    AllClasses.FromLoadedAssemblies().Where(
      t => t.Namespace == "InterceptionHOL"),
    WithMappings.FromAllInterfaces,
    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
entlib.support@live.com
Enterprise Library support engineer
Support How-to