Exception Shielding for generic and specific exceptions

Topics: Exception Handling Application Block
Jan 2, 2010 at 5:09 PM
Hi,
I'm currently using the Exception Handling Block to shield my WCF web service using the following configuration (two specific exceptions and the generic Exception type):
      <add name="ServiceLayerPolicy">
        <exceptionTypes>
          <add type="System.ArgumentException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="ThrowNewException" name="ArgumentException">
            <exceptionHandlers>
              <add logCategory="Server Exceptions" eventId="101" severity="Error"
                title="Argument Exception" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Logging Handler" />
              <add exceptionMessage="One or more web service parameters are invalid"
                faultContractType="MyWebService.InvalidArgumentFault, MyWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                name="Throw InvalidArgumentFault" />
            </exceptionHandlers>
          </add>
<add type="MyWebService.ValidationException, MyWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" postHandlingAction="ThrowNewException" name="ValidationException"> <exceptionHandlers> <add logCategory="Server Exceptions" eventId="102" severity="Error" title="Validation Exception" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler" /> <add exceptionMessage="Some of the data loaded from the server was invalid. The issue has been logged and is being looked into. Please try again later." faultContractType="MyWebService.ValidationFault, MyWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Throw ValidationFault" /> </exceptionHandlers> </add>
<add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" postHandlingAction="ThrowNewException" name="Exception"> <exceptionHandlers> <add logCategory="Server Exceptions" eventId="100" severity="Error" title="Generic Exception" formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" priority="0" useDefaultLogger="false" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Logging Handler" /> <add exceptionMessage="An error occurred on the server while processing your request. The issue was logged and is being looked into. Please try again later." faultContractType="MyWebService.GenericFault, MyWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Throw GenericFault" /> </exceptionHandlers> </add> </exceptionTypes> </add> What I want to achieve is that when a ValidationException is thrown, a ValidationFault is returned to the client.
Likewise, when an ArgumentException is thrown, an InvalidArgumentFault should be returned. ONLY when another exception was thrown,

the GenericFault should be returned.
The problem with the above configuration is that the GenericFault is ALWAYS returned. When a ValidationException is thrown

the handler for this exception kicks in as expected, logging the exception and rethrowing a FaultContractWrapperException with my ValidationFault.

But it seems that this new exception is sent through all the handlers again and then the generic exception handler kicks in,

logging the fault exception and then wrapping it with a GenericFault thereby losing the more specific ValidationFault.

If I remove the FaultExceptionHandler for the GenericFault, it works as expected. But then I would not be able to notify my clients

of generic error conditions on the server (at least not in a controlled way).
Does anyone have a solution for this? I guess I'm missing something quite basic here.
Jan 4, 2010 at 10:29 AM

Hi,

Do you have a quick repro project for this one?

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 5, 2010 at 4:52 AM

Hi,

I have successfully created a working sample out of the config that you've provided. But I haven't reproduce your problem. It is working fine as you've described. I can send you my sample solution, just hit us a mail and we'll send back the sample.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 21, 2010 at 2:34 PM

Hi,

about two weeks ago, I sent you a sample project. This project resembles my project setup and on my system it also showed the behavior described above.
Have you been able to reproduce the problem using my sample?

Regards
Andreas

Jan 22, 2010 at 5:38 AM

Hi,

Yes i have repro the error using your sample project. I have modified your sample solution and is now working fine. I already sent it back to you.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 22, 2010 at 10:40 AM
Edited Jan 22, 2010 at 12:34 PM

Hi,

thanks for sending me the modification.
To be sure that I understand this correctly, does that mean that whenever a service uses exception shielding, you have to omit the call to ExceptionPolicy.HandleException() in the outermost block to prevent an exception from going through the handlers twice? If so then this fact is not made clear in the MSDN documentation (or I simply misunderstood it).

Regards
Andreas

Jan 25, 2010 at 1:18 AM

Yes, that's for WCF services as the ExceptionShielding attribute takes care of that.

 

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