user438975
user438975

Reputation: 263

Return WCF Custom Fault Exception

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?

Upvotes: 7

Views: 13121

Answers (4)

Milan Raval
Milan Raval

Reputation: 1890

Hope this example on Code Project can help you

Upvotes: 2

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

What is faultNamespace? The third parameter of CreateMessage should be action (faultException.Action). Also don't forget that your Fault class must be valid DataContract - in case of default serialization members must be public.

Upvotes: 2

Terrance
Terrance

Reputation: 11872

Steps to create a custom fault contract

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

Upvotes: 3

Roy Dictus
Roy Dictus

Reputation: 33149

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);

Upvotes: 3

Related Questions