How to configuring a Exception Policy using Interception in config file

Sep 28, 2011 at 12:18 PM

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 29, 2011 at 4:21 AM
Edited Sep 29, 2011 at 4:28 AM

Hi,

The ExceptionPolicyImpl class doesn't have a constructor that accepts only a single parameter. If you will look into the source code:

/// <summary>
        /// Initializes a new instance of the <see cref="ExceptionPolicyImpl"/> class with the policy name and a set of policy entries.
        /// </summary>
        /// <param name="policyName">The policy name.</param>
        /// <param name="policyEntries">A set of <see cref="ExceptionPolicyEntry"/> objects.</param>
        public ExceptionPolicyImpl(string policyName, IEnumerable<ExceptionPolicyEntry> policyEntries)
            : this(policyName, policyEntries.ToDictionary(e => e.ExceptionType))
        {
        }

        /// 
        /// <summary>
        /// Initializes a new instance of the <see cref="ExceptionPolicyImpl"/> class with the policy name and a dictionary of policy entries.
        /// </summary>
        /// <param name="policyName">The policy name.</param>
        /// <param name="policyEntries">A set of <see cref="ExceptionPolicyEntry"/> objects.</param>
        public ExceptionPolicyImpl(string policyName, IDictionary<Type, ExceptionPolicyEntry> policyEntries)
        {
            if (policyEntries == null) throw new ArgumentNullException("policyEntries");
            if (string.IsNullOrEmpty(policyName)) throw new ArgumentException(Resources.ExceptionStringNullOrEmpty, "policyName");

            this.policyEntries = policyEntries;
            this.PolicyName = policyName;

            InjectPolicyNameIntoEntries();
        }

If you want to register the ExceptionPolicyImpl class, you also need to specify the policyEntries in your constructor injection. Hope this helps.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 29, 2011 at 11:26 AM

Hi.

Yes, you right. ExceptionPolicyImpl has constructor with two parameters. But I don't known how describe Dictionary in unity config...

Also I look http://msdn.microsoft.com/en-us/library/ff660909(v=pandp.20).aspx This document contains errors, but it's not important...

As I understood I should specify PolicyName from ExceptionHandling block for injection exception in my class.

MSDN
ExceptionPolicyImpl myPolicy 
    = myContainer.Resolve<ExceptionPolicyImpl>("MyExceptionPolicy"); 
myContainer.Configure<Interception>()
           .AddPolicy("MyPolicy")
               .AddMatchingRule<TypeMatchingRule>(new InjectionConstructor("My.Order.Object",
                        true))
               .AddCallHandler<ExceptionCallHandler>
                ("MyExceptionPolicy", new ContainerControlledLifetimeManager());

I do this.
IUnityContainer container = new UnityContainer();
container.LoadConfiguration();

ExceptionPolicyImpl impl = container.Resolve<ExceptionPolicyImpl>("exceptionHandling");
After this I get instance ExceptionPolicyImpl that containt ExceptionPolicyEntry. "exceptionHandling" - it's policy name from ExceptionHandling Block. Unity build ExceptionPolicyEntry by seting from config file.
Then I register instance ExceptionPolicyImp with name "exPolicyImpl" in unity container. 
container.RegisterInstance("exPolicyImpl", impl, new ContainerControlledLifetimeManager());
In setting for callHandler I specify that it dependency from "exPolicyImpl".
<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>

But when calling constructor for ExceptionCallHandler, unity try create new instance ExceptionPolicyImpl instead of using already existing (exPolicyImpl)...

Sep 30, 2011 at 7:25 AM

Hi,

I see. I was able to finally understand the scenario. In that case, you still need to specify in your config the exception policy to use (enterprise library exception handling), basically, its more of a combined enterprise library and unity configuration. I hope this link will get you started.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Jan 11, 2012 at 8:03 PM

Hi Noel,

Thanks for the posting. I tried to run the code, but it kept broken at "throw new NotImplementedException".  When I "View Details", the stacktrace suggests exception at "...<Process_DelegateImplentation>__1 (IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)". Continuing running the app, I see that the exceptionPolicy is still being executed as I can see the "oops! An Invalid operation occured!" msg

If I replace InterfaceInterceptor with TransparentProxyInterceptor, it works just fine without the code bring broken.

I search over the internet and were unable to find a good explaination. COuld you help? Thanks a lot

Joanne

Jan 11, 2012 at 8:09 PM

Sorry I posted at the wrong thread. I meant to post it at http://entlib.codeplex.com/discussions/284446 :(   --Joanne