How to configuring a Exception Policy using Interception in config file

Sep 28, 2011 at 11:01 AM

My config file:

 <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="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common"/>
      <extension type="Interception"/>

      <register name="exPolicyImpl" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling">
        <constructor>
          <param name="policyName" value="exceptionHandling"/>          
        </constructor>
      </register>
      
      <register type="ConsoleApplication1.IConsoleManager, ConsoleApplication1" mapTo="ConsoleApplication1.ConsoleManager, ConsoleApplication1">
        <interceptionBehavior type="PolicyInjectionBehavior"/>
        <interceptor type="InterfaceInterceptor"/>
      </register>

      <interception>
        <policy name="MyPolicy">
          <matchingRule name="exceptionHandling" type="TypeMatchingRule">
            <constructor>
              <param name="typeName">
                <value value="ConsoleApplication1.IConsoleManager" />
              </param>
            </constructor>
          </matchingRule>
          <callHandler name="Exception Handling Call Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling">
            <lifetime type="singleton" />
            <constructor>
              <param name="exceptionPolicy" dependencyName="exPolicyImpl" />
            </constructor>
          </callHandler>
        </policy>
      </interception>
    </container>
  </unity>

In code I try resolver IConsoleManager:

IUnityContainer container = new UnityContainer(); 
container.LoadConfiguration(); 
manager = container.Resolve(); 

but get Exception: Configuration is incorrect, the type Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl does not have a constructor that takes parameters named policyName.

I specify that the "exceptionPolicy" depends from "exPolicyImpl". Then I try resolve ExceptionPolicyImpl in code.

<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="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common"/>
      <extension type="Interception"/>      
      
      <register type="ConsoleApplication1.IConsoleManager, ConsoleApplication1" mapTo="ConsoleApplication1.ConsoleManager, ConsoleApplication1">
        <interceptionBehavior type="PolicyInjectionBehavior"/>
        <interceptor type="InterfaceInterceptor"/>
      </register>

      <interception>
        <policy name="MyPolicy">
          <matchingRule name="exceptionHandling" type="TypeMatchingRule">
            <constructor>
              <param name="typeName">
                <value value="ConsoleApplication1.IConsoleManager" />
              </param>
            </constructor>
          </matchingRule>
          <callHandler name="Exception Handling Call Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling">
            <lifetime type="singleton" />
            <constructor>
              <param name="exceptionPolicy" dependencyName="exPolicyImpl" />
            </constructor>
          </callHandler>
        </policy>
      </interception>
    </container>
  </unity>


My code: 

IUnityContainer container = new UnityContainer();
container.LoadConfiguration(); 
//Revolve ExceptionPolicyImpl. No Exception. 
ExceptionPolicyImpl impl = container.Resolve("exceptionHandling"); 
//Register instance in container 
container.RegisterInstance("exPolicyImpl", impl, new ContainerControlledLifetimeManager()); 
// but when try create ConsoleManager I get the Exception again... 
manager = container.Resolve();

Configuration for Exception Block/Logging Block exist and working fine.

Sep 28, 2011 at 11:28 AM

Hi,

You can also try to ask this to Unity Forum (http://unity.codeplex.com/discussions) since this is more of a Unity question.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us