How to map the exceptions in a fault contract?

Topics: General discussion
Feb 4, 2010 at 3:25 PM

In my service's interface, I am mentioning what type of fault/exception i might receive. Here in the below code, ServiceTempContract is my fault contract.

  [ServiceContract]
    public interface IService
    {
        [OperationContract]
        [FaultContract(typeof(ServiceTempContract))]
        void DoSomething();
    }

and in my client, i am catching "FaultException<ServiceTempContract>". 

Can I have many exceptions mapped to a single method?

I might receive some other exception in the method "DoSomething()", say, OrderTempContract ( which is another fault contract in my WCF). How can i specify that my method might catch either of these two exceptions?

Hope i have explained clearly. If i am missing anything, please let me know.

 

 

Feb 5, 2010 at 3:00 AM

Hi,

Can you try something like this:

        [OperationContract]
        [FaultContract(typeof(ServiceFault))]
        [FaultContract(typeof(InvalidArgumentFault))]
        [FaultContract(typeof(GenericExceptionFault))]
        [FaultContract(typeof(ValidationFault))]
        void DoSomething();
Then on your client... you can catch,
            catch (FaultException ex)
            {
               
            }
Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Feb 5, 2010 at 2:02 PM

Instead of mentioning all data contracts, I think we can have 

 [FaultContract(typeof(FaultException))]

and on client... 
            catch (FaultException ex)
{

}

Does it lead to any other problems?


Feb 8, 2010 at 6:45 AM
Edited Feb 8, 2010 at 6:47 AM

It's a bit weird, what  happens now to your other FaultContracts then? 

 

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

Feb 8, 2010 at 8:53 AM
Yes, I agree, we should specify the contracts the method might lead to instead of specifying FaultException.

Because, we might not know the exact reason of the failure and so we can't take proper action action in client.

Now, I have another doubt; if any exception occurs in the service, and we are throwing a fault instead of exception, the channel stays open in the client; what I would like to do is, close the channel in the service itself if any major exceptions occur .Is it possible to close the existing channel in service?

Feb 10, 2010 at 1:11 AM

I think you need to post this in a WCF forum like this - http://social.msdn.microsoft.com/Forums/en/wcf/threads/

 

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