Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return WCF Custom Fault Exception

Tags:

wcf

I am having bit of trouble returning custom fault exception from wcf service. The client application talking to wcf service gets – contract mismatch error. Here is my fault contract defined in the service:

public partial class Fault
{
    string codeField;
    string messageField;
    string detailsField;
}

On error in the service I am creating custom fault exception as following:

public void ProvideFault(Exception exception, MessageVersion version, ref Message       refMessage)
{
      mynamespace.Fault customFault = new mynamespace.Fault()
      {
           code = "Service Error",
           message = "Error Message",
           details = "Error Message"
      };

      FaultException<mynamespace.Fault> faultexception = new   FaultException<mynamespace.Fault>( customFault );

      MessageFault messageFault = faultexception.CreateMessageFault();

      refMessage = Message.CreateMessage( version, messageFault, faultNamespace );
}

This code causes contract mismatch error in client. Can anyone please help me finding what is wrong?

like image 496
user438975 Avatar asked May 24 '11 13:05

user438975


People also ask

How to throw Fault exception in WCF?

To send an undeclared SOAP fault, throw a System. ServiceModel. FaultException object (that is, not the generic type FaultException<TDetail>) and pass the string to the constructor. This is exposed to the WCF client applications as a thrown System.

How do you handle a fault exception in C#?

In a service, use the FaultException class to create an untyped fault to return to the client for debugging purposes. In a client, catch FaultException objects to handle unknown or generic faults, such as those returned by a service with the IncludeExceptionDetailInFaults property set to true .

What is the difference between fault exception and regular dot net exception?

Exceptions are used to communicate errors locally within the service or the client implementation. Faults, on the other hand, are used to communicate errors across service boundaries, such as from the server to the client or vice versa.


2 Answers

Specify what type of fault you need to return (for example, create a UserMismatchFault class), then throw it like so:

throw new FaultException<UserMismatchFault>(myFault);
like image 184
Roy Dictus Avatar answered Sep 29 '22 02:09

Roy Dictus


Steps to create a custom fault contract

http://bloggingabout.net/blogs/jpsmit/archive/2007/03/21/wcf-fault-contracts.aspx

like image 26
Terrance Avatar answered Sep 29 '22 00:09

Terrance