Click here to Skip to main content
15,881,424 members
Articles / .NET

Using FaultContract to Throw Custom Error Information from WCF Service

Rate me:
Please Sign up or sign in to vote.
4.81/5 (6 votes)
4 Dec 2011CPOL2 min read 41.3K   14   2
This article explains how to handle/control the exception message that a service sends to a client. It also explains how you can use faults to throw custom error messages to a client.

Introduction

This article covers the following areas:

  1. How to enable/disable sending the exception details from WCF service to client
  2. How to use faults to throw custom error messages to client

Details

1. How to enable/disable sending the exception details from WCF service to client

If you throw an exception from WCF service, WCF environment automatically converts the exception into FaultException. The information that the client receives depends on the endpoint behavior. See the following two scenarios:

When Behavior contains <serviceDebug includeExceptionDetailInFaults="False" />:

From the WCF service, throw the exception as shown below:

C#
throw new Exception("Custom Exception");

At client side, you will get FaultException with message, “The server was unable to process the request due to an internal error…”. In this case, the user will not come to know the exception that has been thrown.

When Behavior contains <serviceDebug includeExceptionDetailInFaults="True" />:

From the WCF service, throw the exception as shown below:

C#
throw new Exception("Custom Exception");

At client side, you will get FaultException with message “Custom Exception”.

Note: In production, you have to keep includeExceptionDetailInFaults=”False” in order to restrict the service from sending sensitive information to client.

2. How to use faults to throw custom error messages to client

From the above, we understood that you can’t throw custom error messages as exceptions from a service. In order to throw custom error messages, you have to define a data contract type and throw that as a fault. See the following example.

Create a data contract as follows:

C#
[DataContract]
public class OrderFault
{
    [DataMember]
    public int OrderId { get; set; } 

    [DataMember]
    public string Message { get; set; }
}

In order to throw Fault to client, we have to specify the expected faults above the method/operation using the FaultContractAttribute as follows:

C#
[OperationContract]
[FaultContract(typeof(OrderFault))]
OrderType GetOrder(int orderId);

In the method definition, throw the Fault as follows:

C#
OrderFault fault = new OrderFault{OrderId = 10, Message="Fault"};                
throw new FaultException<OrderFault>(fault, "Order Fault");

Now at client side, you have to catch the fault as follows:

C#
catch (FaultException<OrderFault> ex)
{ 
}

Inside catch, you can get the exception details using ex.Detail. In our case, it's OrderFault.

That’s it. In this way, you can throw custom error message as fault from WCF service and use in client.

Note: Even if you throw fault from operation, if you don’t specify the expected Fault for the operation, at client side you will not get the fault.

History

  • V1.0.0.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionnice article Pin
dotnettechnologyexperts22-Feb-14 4:42
dotnettechnologyexperts22-Feb-14 4:42 
GeneralThanks for the info Pin
Rameez Ahmed Sayad10-Mar-13 5:37
Rameez Ahmed Sayad10-Mar-13 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.