Exception Shielding Handling generic FaultException

Topics: Exception Handling Application Block
May 11, 2007 at 6:06 PM
Hello!

I have an architecture with multiple logic layers that communicates together through WCF services and I use the Exception Shielding pattern to manage the exception handling.

Let's say I have a client that calls a service on layer A and that service calls another service on layer B. The layer B fails for any reason and the exception is correctly managed by its own exception Shielding. So the Service on layer B returns an appropriate System.ServiceModel.FaultException<FaultContractB> to the service on layer A. I don't want the service on layer A to catch the System.ServiceModel.FaultException<FaultContractB>, I wan't the exception Shielding on the Layer A to manage this. My problem is that the Exception Shielding catches a simple System.ServiceModel.FaultException (not the generic System.ServiceModel.FaultException'1 type)...

Now let's say I have two distinct type of FaultException on layer B (how about FaultContractB1 and FaultContractB2). How will the Layer A be able to distinct them if the Exception Shielding handles the same type(System.ServiceModel.FaultException) for both?

I'm afraid that I will have to catch each of the FaultException Type on the Layer A, and throw new custom exceptions that will correspond to each of these types.

Am I missing something?

Thank you!
Jun 13, 2007 at 2:31 PM
Edited Jun 13, 2007 at 2:33 PM
I found the error...

In the exception policy block I used System.ServiceModel.FaultException'1<type>....

what worked is System.ServiceModel.FaultException'1<<type>>....





Crackity_Jones wrote:
Hello!

I have an architecture with multiple logic layers that communicates together through WCF services and I use the Exception Shielding pattern to manage the exception handling.

Let's say I have a client that calls a service on layer A and that service calls another service on layer B. The layer B fails for any reason and the exception is correctly managed by its own exception Shielding. So the Service on layer B returns an appropriate System.ServiceModel.FaultException<FaultContractB> to the service on layer A. I don't want the service on layer A to catch the System.ServiceModel.FaultException<FaultContractB>, I wan't the exception Shielding on the Layer A to manage this. My problem is that the Exception Shielding catches a simple System.ServiceModel.FaultException (not the generic System.ServiceModel.FaultException'1 type)...

Now let's say I have two distinct type of FaultException on layer B (how about FaultContractB1 and FaultContractB2). How will the Layer A be able to distinct them if the Exception Shielding handles the same type(System.ServiceModel.FaultException) for both?

I'm afraid that I will have to catch each of the FaultException Type on the Layer A, and throw new custom exceptions that will correspond to each of these types.

Am I missing something?

Thank you!


Jul 17, 2007 at 10:46 PM
Hello,
I would also like to catch generic FaultExceptions like you propose above, but as of yet have been unsuccessfully. There is no means to enter in a generic FaultException of type <xxx> in the Enterprise configuration tool and I can't seem to manually edit the policy block as you have shown above. The xml chokes when I use "<type>" complaining about the use of the '<' and '>' symbols and I have tried using < and > and square brackets [[, etc.. Can you help a guy out?
Jul 18, 2007 at 12:16 PM
Edited Jul 18, 2007 at 1:15 PM
Hi Kevin,

A single '[' should work. This is a sample configuration snippet:

[snippet deleted, as it was incorrect]

Hope this helps,
Fernando
Jul 18, 2007 at 12:16 PM
Hi,

Here is the way that works with me...

Sorry, please replace the << and >> by square bracks ( [[ ), I cannot use them in the post as it will be render as a link

<add type=System.ServiceModel.FaultException`1<<Project.Services.ExceptionHandling.Fault.UnhandledExceptionFaultContract, Project, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null>>, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 postHandlingAction="ThrowNewException" name="UnhandledExceptionFaultContract">

So don't forget
- the '1 to specify the generic type of fault exception
- the double square bracks
- To specify the assembly information both in the generic type and the FaultException type

Hope it will work for you
Jul 18, 2007 at 12:19 PM
Wow fsimonazzi, we post at the same time with almost two opposite answers...

For me the single square brack '[' never worked...


Jul 18, 2007 at 12:54 PM
Hi,

I'm afraid I did post the repro too hastily. The real solution is using '[[' as you describe; I was partially misled by the '<<' in the original post.

<exceptionTypes>
<add type="System.ServiceModel.FaultException`1[[System.Object, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" name="exception">
<exceptionHandlers>
<add faultContractType="System.Object"
exceptionMessage="" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
name="Fault Contract Exception Handler" />
</exceptionHandlers>
</add>
</exceptionTypes>

Thanks for the correction,
Fernando
Jul 18, 2007 at 6:39 PM
OK, been playing with this most of the day....
Following the advice here I have added:
<add type="System.ServiceModel.FaultException`1<<MyServices.HandledFault1, MyServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null>>, System.ServiceModel, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null"
postHandlingAction="ThrowNewException" name="HandledFault1">
<exceptionHandlers>
<add faultContractType="MyServices.HandledFault2, MyServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
exceptionMessage="" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF.FaultContractExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.WCF, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null"
name="Another Handled Fault Contract Exception Handler" />
</exceptionHandlers>
</add>

NOTE: I have done as others and replaced the '[[' with '<<'

When I add this to the policy block my sandbox blows up. It has problems adding this one to the policy list because it throws an error when it can't convert to the proper type. Any ideas?
Jul 18, 2007 at 6:51 PM
Hi Kevin,

What do you mean by "my sandbox blows up"? Do you get an exception? If that's the case, you should provide the exception message and the stack trace.
I noticed your configuration references a the fault handler from the unsigned assembly, but without the exception this is not enough to help you diagnose the issue.

Fernando
Jul 18, 2007 at 6:54 PM
OK, just figured out I didn't have the PublicKeyToken set properly for the System.ServiceMode.FaultException`1, working now.... Thanks for all the help.