Default configuration for Exception Management?

May 6, 2010 at 12:38 PM

I am trying to do a what I thought was a simple test of Exception Management, but I can't find the required configuration.

 

 

 

This is the code I am using to try and get an instance of the ExceptionManager:   

 

            unityContainer = new UnityContainer();
            var configurator = new UnityContainerConfigurator(unityContainer);
            EnterpriseLibraryContainer.ConfigureContainer(configurator, ConfigurationSourceFactory.Create());
            ExceptionManager exMgr = EnterpriseLibraryContainer.Current.GetInstance();

However, I get the following error:

  Microsoft.Practices.ServiceLocation.ActivationException was unhandled by user code
Message=Activation error occured while trying to get instance of type ExceptionManager, key ""
 
I assume this is due to incorrect configuration, but I can't find anywhere in the documentation that it says what the correct configuration is.



May 6, 2010 at 2:18 PM

Did you add the Exception Handling block in your config?  Actually, the last line of code would be sufficient to resolve an instance of ExceptionManager, except that the GetInstance should be GetInstance<ExceptionManager> or GetInstance(typeof(ExceptionManager))

If you want to use an instance of IUnityContainer, you include the first 3 lines of code and resolve from the unityContainer variable instead of getting it from EnterpriseLibraryContainer.Current

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
 entlib.support@avanade.com

May 6, 2010 at 8:30 PM

I tried the change you suggested, but I still get the same error. Any ideas?

It fails on the GetInstance<ExceptionManager> call.

This is the code:

    [TestClass]
    public class ConfigurationFixture
    {
        private IUnityContainer unityContainer;

        [TestInitialize]
        public void Init()
        {
            unityContainer = new UnityContainer();
            var configurator = new UnityContainerConfigurator(unityContainer);
            EnterpriseLibraryContainer.ConfigureContainer(configurator, ConfigurationSourceFactory.Create());

        }

        [TestMethod]
        public void SqlExceptionWrapHandlerSuccess()
        {
            // Create a mimic SqlException
            var dummyEx = SqlExceptionCreator.CreateSqlException("Dummy message", new Error (1601, "Message 1601"));

            ExceptionManager exMgr = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>();
            exMgr.Process(() => dummyEx, 
                "DataAccessPolicy");
        }
    }

 

This is the config file I am using:

<?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.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    <section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" />
    <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.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="Enterprise Library Logging" formatter="Text Formatter"
        log="" machineName="." traceOutputOptions="None" />
    </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="Event Log 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="DataAccessPolicy">
        <exceptionTypes>
          <add name="SqlException" type="System.Data.SqlClient.SqlException, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Common.Exceptions.SqlExceptionWrapHandler, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb3f7e851625ad3"
                name="SqlExceptionWrapHandler" defaultExceptionMessage="SQL Error" defaultWrapExceptionType="System.Exception" errorCodeNotConfiguredBehaviour="WrapWithDefaultException">
              </add>
            </exceptionHandlers>
          </add>
          <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.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" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
  <dataConfiguration defaultDatabase="AggregationDB" />
  <connectionStrings>
    <add name="AggregationDB" connectionString="Data Source=(LOCAL)\SharepointSQL;Initial Catalog=AggregationDB;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

<!--l version="1.0" encoding="utf-8"-->

May 6, 2010 at 8:52 PM

So it appears the problem is in it trying to resolve the custom ExceptionHandler I want to use: SqlExceptionWrapHandler.

The message in the exception is:

Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManagerImpl,ExceptionManager.__default__ (mapped from Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager, (none))
Resolving parameter "exceptionPolicies" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManagerImpl(System.Collections.Generic.IEnumerable'1[[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] exceptionPolicies, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Instrumentation.IDefaultExceptionHandlingInstrumentationProvider instrumentationProvider)
Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl,DataAccessPolicy
Resolving parameter "policyEntries" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl(System.String policyName, System.Collections.Generic.IEnumerable'1[[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] policyEntries)
Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry,DataAccessPolicy.SqlException
Resolving parameter "handlers" of constructor Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyEntry(System.Type exceptionType, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.PostHandlingAction postHandlingAction, System.Collections.Generic.IEnumerable'1[[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.IExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]] handlers, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Instrumentation.IExceptionHandlingInstrumentationProvider instrumentationProvider)
Resolving Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.IExceptionHandler,DataAccessPolicy.SqlException.SqlExceptionWrapHandler

The last line is where the problem is: the SqlExceptionWrapHandler doesn't exist in DataAccessPolicy.SqlException, but in my custom assembly, which is defined in the config file as:

              <add type="Common.Exceptions.SqlExceptionWrapHandler, Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb3f7e851625ad3"
                name="SqlExceptionWrapHandler" defaultExceptionMessage="SQL Error" defaultWrapExceptionType="System.Exception" errorCodeNotConfiguredBehaviour="WrapWithDefaultException">
              </add>
I have checked with FusLogvw, the Common.dll file is being loaded correctly.
Any other ideas?
May 7, 2010 at 2:01 AM
Edited May 7, 2010 at 2:01 AM

Does your SqlExceptionWrapHandler contains constructors with parameters? If yes, It fails because your custom exception handler isn't configured in the container.   You must register your custom handler in the container either through code or in the config.  Remember that unity uses the constructor with the most number of parameters to create an object.  You can override this behavior by specifying which constructor to use again either thru code or config. 

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
 entlib.support@avanade.com