2

I've got a WCF service that's using Enterprise Library for its Exception Handling and WCF Validation Integration. They don't seem to work together.

What I want is:

  1. If my service throws any kind of exception, WCF Exception Shielding will catch this and throw out a FaultException<MyServiceFault>.
  2. If the incoming message fails the validation, the service should continue to throw out a FaultException<ValidationFault>.

However, I don't know how to tell the Exception Shielding policy to let Validation exceptions through.

I have some Enterprise Library config that turns every exception into a FaultException<MyServiceFault>. If I get EnterpriseLibrary to log the exceptions it catches, it says it gets a

Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractWrapperException

when validation fails. But if add this exception to the config as a new <exceptionType> with a postHandlingAction of None, I still get a FaultException<MyServiceFault> thrown out when validation fails.

Is it possible to use WCF Exception Shielding to catch every exception other than WCF Validation exceptions? If not, I'll have to add tons more config for all the different exceptions that might be thrown - what is the point of exception shielding in this case?

Graham Clark
  • 12,886
  • 8
  • 50
  • 82

2 Answers2

0

Hmm, well although I thought I'd already tried this, the following configuration seems to do the job:

<add name="WCF Exception Shielding">
    <exceptionTypes>
        <add name="FaultException`1" type="System.ServiceModel.FaultException`1[[Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF.ValidationFault, Microsoft.Practices.EnterpriseLibrary.Validation.Integration.WCF, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            postHandlingAction="NotifyRethrow" />
        <add name="All Exceptions" type="System.Exception, mscorlib, 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.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                    faultContractType="MyServiceFault, MyServices"
                    name="Fault Contract Exception Handler" />
            </exceptionHandlers>
        </add>
    </exceptionTypes>
</add>

Any exception gets thrown inside the service code, it's thrown out as a FaultException<MyServiceFault>, except if the WCF Validation Application Block throws an exception, then everything works as normal and the client receives a FaultException<ValidationFault>.

Graham Clark
  • 12,886
  • 8
  • 50
  • 82
0

There's a bug in the library that would prevent the solution you provided from functioning. The workaround is not to change the default policy name of the exception shielding block.

Refer to this link to add vote to this bug.

mrtaikandi
  • 6,753
  • 16
  • 62
  • 93
  • Whilst it is true that having anything other than "WCF Exception Shielding" will cause it all to fail, I *am* using this policy name. The config in my answer does work! – Graham Clark Dec 27 '11 at 18:57