How to add ExceptionCallHandler to interception policy using configuration?

Topics: Exception Handling Application Block, Policy Injection Application Block
May 2, 2011 at 9:44 PM

Hello,

For a couple of days I've been fighting this problem and the best result I've got so far was an exception which contained a message "The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate." Below is the configuration file of a sample app that I tried to put together only for this issue. Can you please tell me what am I doing wrong and how to fix it. I would prefer to stick to config file solutions.

I'll appreciate your help here!

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    
    <configSections>
        <section name="unity"
                 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
        <section name="validation"
                 type="Microsoft.Practices.EnterpriseLibrary.Validation.Configuration.ValidationSettings, Microsoft.Practices.EnterpriseLibrary.Validation, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 requirePermission="true" />
        <section name="loggingConfiguration"
                 type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 requirePermission="true" />
        <section name="exceptionHandling"
                 type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 requirePermission="true" />
    </configSections>

    <loggingConfiguration name=""
                          tracingEnabled="true"
                          defaultCategory="General"
                          revertImpersonation="false">
        
        <listeners>
            <add name="A Rolling Flat File"
                 type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 fileName="c:\Logs\ErrorCallHandlerTest\Events.log"
                 footer="--------------------------"
                 formatter="Text"
                 header="**************************"
                 rollInterval="Midnight"
                 rollSizeKB="10000"
                 filter="All" />
            <add name="A Rolling Flat File Loggers Errors"
                 type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 fileName="c:\Logs\ErrorCallHandlerTest\Unprocessed Errors.log"
                 footer="--------------------------"
                 formatter="Text"
                 header="**************************"
                 rollInterval="Midnight"
                 rollSizeKB="10000"
                 filter="Warning" />
            <add listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.SystemDiagnosticsTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 type="System.Diagnostics.DefaultTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                 traceOutputOptions="DateTime, ThreadId, Callstack"
                 filter="Warning"
                 name="VS Output Pane" />
        </listeners>
        
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                 template="{property(ActivityId)}&#xA;{timestamp(local:FixedFormatTime)} {category}: {severity} {eventid} @ {localMachine}&#xA;{message}&#xA;Process: {localProcessName}&#xA;{dictionary(    {key} - {value}{newline})}"
                 name="Text" />
        </formatters>
        
        <categorySources>
            <add switchValue="All"
                 name="General">
                <listeners>
                    <add name="A Rolling Flat File" />
                </listeners>
            </add>
        </categorySources>
        
        <specialSources>
            <allEvents switchValue="Verbose"
                       name="All Events">
                <listeners>
                    <add name="VS Output Pane" />
                </listeners>
            </allEvents>
            <notProcessed switchValue="Warning"
                          name="Unprocessed Category">
                <listeners>
                    <add name="A Rolling Flat File Loggers Errors" />
                </listeners>
            </notProcessed>
            <errors switchValue="Warning"
                    name="Logging Errors &amp; Warnings">
                <listeners>
                    <add name="A Rolling Flat File Loggers Errors" />
                </listeners>
            </errors>
        </specialSources>
        
    </loggingConfiguration>
    
    <exceptionHandling>
        <exceptionPolicies>
            <add name="Policy">
                <exceptionTypes>
                    <add name="ArgumentNullException"
                         type="System.ArgumentNullException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                         postHandlingAction="None">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler"
                                 type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                 logCategory="General"
                                 eventId="101"
                                 severity="Error"
                                 title="Program"
                                 formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                 priority="1" />
                        </exceptionHandlers>
                    </add>
                    <add name="ArgumentException"
                         type="System.ArgumentException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                         postHandlingAction="None">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler"
                                 type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                 logCategory="General"
                                 eventId="100"
                                 severity="Error"
                                 title="Program"
                                 formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                 priority="1" />
                        </exceptionHandlers>
                    </add>
                    <add name="InvalidOperationException"
                         type="System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                         postHandlingAction="None">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler"
                                 type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                 logCategory="General"
                                 eventId="200"
                                 severity="Error"
                                 title="Program"
                                 formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                 priority="2" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>

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

        <!-- assemblies and namespaces to look into -->
        <assembly name="Microsoft.Practices.EnterpriseLibrary.PolicyInjection"/>
        <namespace name="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers"/>

        <assembly name="Microsoft.Practices.EnterpriseLibrary.Validation"/>
        <namespace name="Microsoft.Practices.EnterpriseLibrary.Validation.PolicyInjection"/>

        <assembly name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
        <namespace name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
        <namespace name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection"/>

        <assembly name="ErrorCallHandler"/>
        <namespace name="ErrorCallHandlerTest"/>
        
        <container>
            <!-- load the interception container extension in this container instance -->
            <extension type="Interception"/>

            <register type="ITest"
                      mapTo="TestClass">
                <interceptionBehavior type="PolicyInjectionBehavior"/>
                <interceptor type="InterfaceInterceptor"/>
            </register>

            <register type="ExceptionPolicyImpl">
                <constructor>
                    <param name="policyName"
                           value="Policy"/>
                </constructor>
            </register>

            <interception>
                <policy name="interception">
                    <matchingRule name="forAll"
                                  type="NamespaceMatchingRule">
                        <constructor>
                            <param name="namespaceName"
                                   value="ErrorCallHandlerTest"/>
                            <param name="ignoreCase"
                                   value="false"/>
                        </constructor>
                    </matchingRule>

                    <callHandler name="ExceptionHandling"
                                 type="ExceptionCallHandler">
                        <lifetime type="singleton"/>
                        <constructor>
                            <param name="exceptionPolicy"
                                   type="ExceptionPolicyImpl"/>
                        </constructor>
                    </callHandler>

                    <callHandler name="Validation"
                                 type="ValidationCallHandler">
                        <lifetime type="singleton"/>
                        <constructor>
                            <param name="ruleSet"
                                   value=" "/>
                            <param name="specificationSource"
                                   value="Attributes"/>
                        </constructor>
                    </callHandler>

                </policy>

            </interception>
        </container>
    </unity>

</configuration>
May 3, 2011 at 4:43 PM

Hi,

Where exactly the exception was raised? I was getting a "Configuration is incorrect, the type Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl does not have a constructor that takes parameters named policyName." exception, but this is due to the section below;

      <register type="ExceptionPolicyImpl">
        <constructor>
          <param name="policyName"
                 value="Policy"/>
        </constructor>
      </register>
With this see if this thread - http://unity.codeplex.com/discussions/216178 would be helpful to your problem. HTH

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
 entlib.support@avanade.com

May 3, 2011 at 7:12 PM

Yes, you are right, I was changing many things around and this was also one of the exceptions that I've got.

However the link was very useful (although not exactly intuitive or well documented else where) and now it works. I just don't understand why I could not find it... Anyway thank you very much!

Val