How to config interception in cfg file?

Feb 9, 2012 at 7:01 AM

I have this running, part of the config is code-based, how to make them all in the cfg file?

App.Config:
<configSections>
    <section name="unity" type="...UnityConfigurationSection, ..."/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <sectionExtension type="...InterceptionConfigurationExtension, ..." />

    <container>
      <extension type="Interception" />

      <register type="MyInterceptor.Interceptor, MyInterceptor" />
      <register type="MyContracts.IInterceptable, MyContracts" mapTo="BizComp.BizClass, BizComp" />
    </container>

code:
 ...
        void Init()
        {
            UC = new UnityContainer().LoadConfiguration();

            UC.AddNewExtension<Interception>();
            UC.Configure<Interception>().AddPolicy("testInt")
                .AddMatchingRule<AnyMatchRules>()
                .AddCallHandler<MyInterceptor.Interceptor>()
                ;

            UC.Configure<Interception>().SetInterceptorFor<MyContracts.IInterceptable>(new TransparentProxyInterceptor());
        }
    }

    public class AnyMatchRules : IMatchingRule
    {
        public bool Matches(System.Reflection.MethodBase member)
        {
            return true;
        }
    }

Feb 10, 2012 at 3:13 AM

You can configure your call handler using Policy Injection.  A sample configuration based on your classes would be something like this:

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration"/>
  
    <container>
      
      <extension type="Interception"/>

      <interception>

        <policy name="Policy">
          <matchingRule name="Match" type="UnityCallHandlerConfig.AnyMatchRules, UnityCallHandlerConfig" />
          <callHandler name="MyInterceptor" type="UnityCallHandlerConfig.MyInterceptor, UnityCallHandlerConfig"/>
        </policy>
        
      </interception>

      <register type="UnityCallHandlerConfig.IInterceptable, UnityCallHandlerConfig" mapTo="UnityCallHandlerConfig.BizClass, UnityCallHandlerConfig">
        <interceptor type="TransparentProxyInterceptor"/>
        <policyInjection />
      </register>
    
    </container>
 
  </unity>

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com