System.Exception type handlers overide other exception types handler

Topics: Exception Handling Application Block
Mar 17, 2012 at 10:12 PM

Hi, 

I have the following scenario.

 

I want to have a policy with two exception types (optimisticconcurencyexception and system.exceptions). For the optimisticconcurencyexception  i add a fault handler. For system.exceptions i add a different fault handler and a logging handler. Now if i catch the optimisticconcurencyexception  the library will execute the system.exceptions fault handler and logging handler and not just the fault handler from optimisticconcurencyexception  

I set a breakpoint in the debugger where the exmanager process the method that throw's the exceptions and in code i get the right faultexception.

Am i understandable? Any thoughts on this issue?

 

Thanks 

Alex

Mar 19, 2012 at 4:34 AM

Can you post your configuration?

Thanks,

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

Mar 19, 2012 at 6:15 PM

 

 <configSections>
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    <section name="securityConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Configuration.SecuritySettings, Microsoft.Practices.EnterpriseLibrary.Security, 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" />
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  
  <cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
      <add name="Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
        numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore" />
    </cacheManagers>
    <backingStores>
      <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="NullBackingStore" />
    </backingStores>
  </cachingConfiguration>
  <securityConfiguration defaultSecurityCacheInstance="Security Cache">
    <securityCacheProviders>
      <add type="Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore.CachingStoreProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cache.CachingStore, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        cacheManagerInstanceName="Cache Manager" defaultSlidingSessionExpirationInMinutes="10"
        defaultAbsoluteSessionExpirationInMinutes="60" name="Security Cache" />
    </securityCacheProviders>
  </securityConfiguration>
  <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="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, Callstack" />
      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="D:\trace.log" formatter="Text Formatter" traceOutputOptions="LogicalOperationStack, DateTime, Timestamp, ProcessId, ThreadId, 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="Event Log Listener" />
          <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="First">
        <exceptionTypes>
          <add name="OptimisticConcurrencyException" type="System.Data.OptimisticConcurrencyException, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Concurrency" faultContractType="SL.Faults.ConcurrencyFault, SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Concurrency Fault" />
            </exceptionHandlers>
          </add>
          <add name="MetadataException" type="System.Data.MetadataException, System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <exceptionHandlers>
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="Test Meta fault" faultContractType="SL.Faults.ConcurrencyFault, SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler" />
            </exceptionHandlers>
          </add>
          <add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException">
            <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="6" />
              <add type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                exceptionMessage="All Exceptions" faultContractType="SL.Faults.ConcurrencyFault, SL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                name="Fault Contract Exception Handler" />
            </exceptionHandlers>
          </add>
        </exceptionTypes>
      </add>
    </exceptionPolicies>
  </exceptionHandling>
Mar 20, 2012 at 4:58 AM

>  Now if i catch the optimisticconcurencyexception

Are you actually catching OptimisticConcurrencyException?  I don't think you need to since the block will handle that for you.

I set up a WCF Exception Shielding sample using your configuration but with my own faultContractType and it seems to work fine.  When an OptimisticConcurrencyException is thrown the exception is handled but not logged but when another exception is thrown then the exception is logged and handled as I expect.

Are you using the Process() method?  If you are using exception shielding then you don't need to -- you can just let the exception bubble up and the handler will do the work for you.  My code looks like this:

    [ExceptionShielding("First")]
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            //throw new ArgumentNullException("Nuuuuulllll!");
            throw new System.Data.OptimisticConcurrencyException("I'm feeling a bit pessimistic. :(");
 
           return string.Format("You entered: {0}", value);
        }
    }

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

Mar 20, 2012 at 9:06 AM

Yeah, i am using the exceptionshielding decorator on my service. So, if i use the decorator, i dont need to use process method? i can let the exception bublle to the service and from there the handlers that i set up for the exception will take over?

Thanks

Alex

Mar 20, 2012 at 2:25 PM

That's right.  If you are using the ExceptionShielding decorator with WCF then any unhandled exceptions in code will actually be handled by the exception shielding IServiceBehavior (which hooks into the WCF runtime) so you don't need to explicitly handle exceptions (using try/catch or the Process() method). 

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

Mar 20, 2012 at 7:06 PM

Hey,

I just tried this out. Thanks for your help. Looking forward to applying the library further in my projects :) 

Thumps up

Alex