Please help with configuring ExceptionCallHandler and unity injection

Jun 26, 2012 at 4:23 PM

Hello,

I went through several articles on this site and elsewhere and I thought I am on the right track but I get the following exception:

Resolution of the dependency failed, type = "GBI.Services.PaymentManager.Services.IEntityFactory", name = "(none)".

Exception occurred while: Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container).

Exception is: ResolutionFailedException - Resolution of the dependency failed, type = "Microsoft.Practices.Unity.InterceptionExtension.ICallHandler", name = "73cceebd-fd2a-40f4-8d4f-dc0683df9ccc".

Exception occurred while: while resolving.

Exception is: InvalidOperationException - The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate.

Here is the configuration of my exception handler:

<configuration>
    <exceptionHandling>
        <exceptionPolicies>
            <add name="Log And Rethrow">
                <exceptionTypes>
                    <add name="All Exceptions"
                         type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                         postHandlingAction="NotifyRethrow">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler"
                                 type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging"
                                 logCategory="Exception"
                                 eventId="50"
                                 severity="Error"
                                 title="Enterprise Library Exception Handling"
                                 formatterType="GBI.Services.Common.DumpExceptionFormatter, GBI.Services.Common"
                                 priority="1" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>
</configuration>

And here is the configuration of the injection policy from my container:

<configuration>
    <configSections>
        <section name="unity"
                 type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
    </configSections>

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

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

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

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

        <assembly name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
        <namespace name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling" />
        <namespace name="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection" />
        <container>
            <extension type="EnterpriseLibraryCoreExtension" />
            <extension type="Interception" />
            <interception>
                <policy name="Validated">
                    <matchingRule name="factory"
                                  type="TagAttributeMatchingRule">
                        <constructor>
                            <param name="tagToMatch"
                                   value="Validated"/>
                            <param name="ignoreCase"
                                   value="false"/>
                        </constructor>
                    </matchingRule>

                    <callHandler name="ExceptionHandling"
                                 type="ExceptionCallHandler">
                        <lifetime type="singleton"/>
                        <constructor>
                            <param name="exceptionPolicy"
                                   dependencyName="Log And Rethrow"/>
                        </constructor>
                    </callHandler>

                    <callHandler name="Validation"
                                 type="ParameterValidatingCallHandler">
                        <lifetime type="singleton" />
                        <constructor>
                            <param name="ruleSet"
                                   value=" " />
                            <param name="specificationSource"
                                   value="Attributes"/>
                        </constructor>
                    </callHandler>
                </policy>
            </interception>
        </container>
    </unity>
</configuration>

What am I doing wrong here?

Thank you

Val

Jun 29, 2012 at 8:44 AM

Usually the message indicates that the exception policy name in the policy injection constructor is not correct.  The names match above but I wonder why in the unity configuration I don't see the exception handling section registered?  They should be in the same config file (unless you are doing some other advanced configuration).

For example this config file works for me:

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

  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="Enterprise Library Logging" formatter="Text Formatter"
        log="" machineName="." traceOutputOptions="None" />
      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="trace.log" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="Timestamp: {timestamp}{newline}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;Extended Properties: {dictionary({key} - {value}{newline})}"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Event Log Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="Log And Rethrow">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                logCategory="General" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="0" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <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"/>

      <interception>

        <policy name="Policy">
          <matchingRule name="Match" type="TypeMatchingRule">
            <constructor>
              <param name="typeName" value="BizClass" />
            </constructor>
          </matchingRule>          
          <callHandler name="ExceptionCallHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PolicyInjection.ExceptionCallHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <lifetime type="singleton"/>
            <constructor>
              <param name="exceptionPolicy"
                     dependencyName="Log And Rethrow"/>
            </constructor>
          </callHandler>        
        </policy>
        
      </interception>

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

    </container>
 
  </unity>

</configuration>

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

Jul 2, 2012 at 6:25 PM
Edited Jul 2, 2012 at 6:26 PM

Thanks Randy!

Thanks to your sample, I made only one change and it worked. If you have a minute could you please explain what's the difference:

            <register type="IEntityFactory"
                      mapTo="EntityFactory">
                <lifetime type="singleton" />
                <interceptor type="InterfaceInterceptor"/>
                <interceptionBehavior type="PolicyInjectionBehavior"/> chaged to <policyInjection/>
            </register>
Thank you for you help!
Val
Jul 3, 2012 at 3:25 AM
 <interceptionBehavior type="PolicyInjectionBehavior"/> 

should be functionally equivalent to 

 <policyInjection/>

The policyInjection element is "a shortcut element to enable the policy injection behavior" so there shouldn't be any difference.  I used both with the same results.  If you have a sample to reproduce the issue I can take a look at it if you wish.

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