"Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl\" : The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate.

Topics: Exception Handling Application Block, Logging Application Block
Sep 11, 2012 at 3:47 PM
Edited Sep 12, 2012 at 5:36 PM

Hi Team,

While using Enterprise Library 5.0 for my exception handling and Logging, I am not able to log the exception In case of a DataAcessException or BusinessLogicException, where I am getting the below error. Even after a lot of efforts, I am not able to resolve this issue. I have made sure that I have all the EnterpriseLibrary dll's referred in my project. Can someone please help me:

{"Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl\", name = \"DataAccessPolicy\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type ExceptionPolicyImpl has multiple constructors of length 2. Unable to disambiguate.\r\n-----------------------------------------------\r\nAt the time of the exception, the container was:\r\n\r\n  Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,DataAccessPolicy\r\n"}

Below is my DataAccessExceptionHandler and BusinessExceptionHandler Class

*****************************************************************************************************************

public static class DataAccessExceptionHandler
   {
      public static bool HandleException(ref System.Exception ex)
      {
         bool rethrow = false;
         if ((ex is SqlException))
         {
            SqlException dbExp = (SqlException)ex;
            if (dbExp.Number >= 50000)
            {
               rethrow = ExceptionPolicy.HandleException(ex, "DataAccessCustomPolicy");
               ex = new DataAccessCustomException(ex.Message);
            }
            else
            {
               rethrow = ExceptionPolicy.HandleException(ex, "DataAccessPolicy");
               if (rethrow)
               {
                  throw ex;
               }
            }
         }
         else if (ex is System.Exception)
         {
            rethrow = ExceptionPolicy.HandleException(ex, "DataAccessPolicy");
            if (rethrow)
            {
               throw ex;
            }
         }
         return rethrow;
      }
   } 

public static class BusinessLogicExceptionHandler
   {
      public static bool HandleException(ref System.Exception ex)
      {
         bool rethrow = false;
         if ((ex is DataAccessException) || (ex is DataAccessCustomException))
         {
            rethrow = ExceptionPolicy.HandleException(ex, "PassThroughPolicy");
            ex = new PassThroughException(ex.Message);
         }
         else if (ex is BusinessLogicCustomException)
         {
            rethrow = ExceptionPolicy.HandleException(ex, "BusinessLogicCustomPolicy");
         }
         else
         {
            rethrow = ExceptionPolicy.HandleException(ex, "BusinessLogicPolicy");
         }
         if (rethrow)
         {
            throw ex;
         }
         return rethrow;
      }

   }

Below is a snip from my web.config file:

*****************************************************************************************************************

  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
    <listeners>
      <add name="Rolling Flat File Trace Listener" 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="log\rolling.log" formatter="Text Formatter" rollSizeKB="500"
        maxArchivedFiles="100" traceOutputOptions="DateTime, ProcessId, Callstack" />
      <add name="SPAM Event Viewer Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        source="SPAM" formatter="Text Formatter" traceOutputOptions="DateTime, ProcessId, Callstack" />
      <add name="Database Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        databaseInstanceName="LoggingConnection" writeLogStoredProcName="WriteLog"
        addCategoryStoredProcName="AddCategory" formatter="Text Formatter"
        traceOutputOptions="DateTime, ProcessId, Callstack" />
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.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="Database 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="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>
  <exceptionHandling>
    <exceptionPolicies>
      <add name="DataAccessPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="DataAccessLoggingHandler" 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="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="0" />
              <add name="DataAccessReplaceHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="An error has occurred in Data Layer. &lt;BR /&gt;Please contact your Admin or Support at  with Error ID {handlingInstanceID} if the issue persists."
                replaceExceptionType="SpaMagic.Helpers.ExceptionHandling.DataAccessException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="DataAccessCustomPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                replaceExceptionType="SpaMagic.Helpers.ExceptionHandling.DataAccessCustomException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="PassThroughPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="PassThroughReplaceHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                replaceExceptionType="SpaMagic.Helpers.ExceptionHandling.PassThroughException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="BusinessLogicPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="BusinessLogicLoggingHandler" 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="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="0" />
              <add name="BusinessLogicReplaceHandler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="An error has occurred in Business Layer. &lt;BR /&gt;Please contact your Admin or Support at  with Error ID {handlingInstanceID} if the issue persists."
                replaceExceptionType="SpaMagic.Helpers.ExceptionHandling.BusinessLogicException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="BusinessLogicCustomPolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow">
            <exceptionHandlers>
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                replaceExceptionType="SpaMagic.Helpers.ExceptionHandling.BusinessLogicCustomException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
      <add name="UserInterfacePolicy">
        <exceptionTypes>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add name="UserInterfaceReplaceHandler" 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="Enterprise Library Exception Handling"
                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                priority="0" />
              <add name="Replace Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ReplaceHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="An error has occurred in Interface. &lt;BR /&gt;Please contact your Admin or Support  with Error ID {handlingInstanceID} if the issue persists."
                replaceExceptionType="SpaMagic.Helpers.ExceptionHandling.UserInterfaceException, Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>

 

Sep 11, 2012 at 6:49 PM

Hi folks/Admins,

I am working against time in delivering this, can someone please help me in resolving this issue?

Regards

Himani

Sep 12, 2012 at 1:29 AM

This error occurs when there is no exceptionHandling configuration in the appropriate configuration file (web.config or app.exe.config).

Verify that the posted configuration exists in the deployed web.config.  In the case of nested web.configs verify that the configuration is in an appropriate level for the code.

If that doesn't help, you can create a sample application to reproduce the issue and send it to me.

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

Sep 12, 2012 at 8:13 AM

Have sent you the sample code Randy, where I was able to resolve this issue, but still was not able to make Enterprise Library Exception Handling work...:( I am looking forward to hear from you on what I was doing wrong...

 

Sep 12, 2012 at 4:00 PM

You were missing references to Unity.

The message that was appearing was:

Could not load file or assembly 'Microsoft.Practices.Unity, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

If you add references to: 

Microsoft.Practices.Unity.Configuration.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.Configuration.dll
Microsoft.Practices.Unity.Interception.dll

Then everything should work.  Then you should see:

An error has occurred in Interface. Please contact your Admin or Support at magicmills with Error ID 03a0d313-351d-42dc-b8f1-bab143f90406 if the issue persists.

BTW, NuGet is great at ensuring that all dependencies are added to your project.

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

Sep 12, 2012 at 5:35 PM

Thanks a lot for your time Randy, but unfortunately that did not help...:(. The POC is created with a requirement to use ReplaceHandlers at each layer to replace the exception text at each layer.

I have attached the sample project again with all the references. The issue is when BusinessException or DataException is logged, PassThroughExceptionPolicy is not working. When the exception is thrown from BusinessLogicExceptionHandler or DataAccessExceptionHandler, it is not able to replace the exception text and let it pass-through.

Below is the expectation and the cases which are failing. I am sure if you can have a look at the code, you can point out the gaps. When I use this POC for a solution where UserInterface, BusinessLayer and DataLayer are all in the same project, everything works fine, but with a layered architecture, it is not able to throw exception at the previous layer. All the exceptions are happening at BusinessLogicExceptionHandler which has to throw the exception back to User Interface. Thanks a bunch for all your help in advance...


Scenarios Expected Message Actual Message Works??
Scenario1 Successfully fetched Data from DB Yes
Scenario2 An unknown error has occurred in Data Access Layer while processing your request. Please contact Help Desk Support at X-XXX-XXX-XXXX with Error Token ID 683bf077-42b6-42dc-b74d-0e6be0650f60 Unhandled Exception in BusinessLogicExceptionHandler.cs (PassthroughPolicy not working) and then: An unknown error has occurred in Interface Layer while processing your request. Please contact Help Desk Support at X-XXX-XXX-XXXX with Error Token ID 8b89306f-02fc-4703-a067-25d1d9e25526. No
Scenario4 An unknown error has occurred in Business Logic Layer while processing your request. Please contact Help Desk Support at X-XXX-XXX-XXXX with Error Token ID 012ecf2a-71aa-4d2d-a927-43eb2780882c. Unhandled Exception in BusinessLogicExceptionHandler.cs (PassthroughPolicy not working) No
Scenario5 Unhandled BusinessLogicCustomException in BusinessLogicExceptionHandler.cs No
Scenario6 An unknown error has occurred in Interface Layer while processing your request. Please contact Help Desk Support at X-XXX-XXX-XXXX with Error Token ID d992e447-98fd-47e8-9f9f-8e3ec15ac907. An unknown error has occurred in Interface Layer while processing your request. Please contact Help Desk Support at X-XXX-XXX-XXXX with Error Token ID d992e447-98fd-47e8-9f9f-8e3ec15ac907. Yes

Regards
Himani

From: [email removed]
To: [email removed]
Date: Wed, 12 Sep 2012 08:00:29 -0700
Subject: Re: "Resolution of the dependency failed, type = \"Microsoft.Practices.EnterpriseLibrary.ExceptionHan... [entlib:395129]

From: randylevy
You were missing references to Unity.
The message that was appearing was:
Could not load file or assembly 'Microsoft.Practices.Unity, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.


If you add references to:
Microsoft.Practices.Unity.Configuration.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.Configuration.dll
Microsoft.Practices.Unity.Interception.dll
Then everything should work. Then you should see:

An error has occurred in Interface. Please contact your Admin or Support at magicmills with Error ID 03a0d313-351d-42dc-b8f1-bab143f90406 if the issue persists.


BTW, NuGet is great at ensuring that all dependencies are added to your project.
--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Read the full discussion online.
To add a post to this discussion, reply to this email (entlib@discussions.codeplex.com)
To start a new discussion for this project, email entlib@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com
Sep 12, 2012 at 6:30 PM

When you layer your architecture you are using WCF to communicate between the UI and the business layer.  When dealing with WCF you need to take a different approach for exception handling.  See Fault Handling in WCF Services for more information.

You are probably going to want to use WCF exception shielding which you can read about in the Developer's Guide.

So at the top level of your Business layer you will want to use a FaultContractExceptionHandler to translate the exception to a FaultException<T>.  Then in the UI layer you would catch FaultException<T> (as per the service contract).

Also, see the WCF Exception Shielding With Logging Exception Handler example on the Samples Page.

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

 

Sep 13, 2012 at 6:04 AM

Thanks for the pointers Randy, after defining FaultContract and making small changes in my BusinessExceptionHandling, I am able to make it work as needed.

Sep 13, 2012 at 9:10 AM

Good to hear!

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