Return WCF Custom Fault Exception

wcf
12,970

Solution 1

Steps to create a custom fault contract

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

Solution 2

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

Solution 3

Hope this example on Code Project can help you

Solution 4

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.

Share:
12,970
user438975
Author by

user438975

Updated on August 08, 2022

Comments

  • user438975
    user438975 over 1 year

    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?