Over a million developers have joined DZone.

WCF REST tip 2: WebFaultException

DZone's Guide to

WCF REST tip 2: WebFaultException

· ·
Free Resource

SOAP supports propagating exceptions from the service to the client. This is called “faults” and in WCF this is represented by the FaultException class. Exceptions in REST services are represented by a HTTP status code >= 400. Not a lot of people seem to be aware that WCF supports setting HTTP status error codes by throwing a WebFaultException. Most people doing REST in WCF seem to use the StatusCode property of the OutgoingResponse object that can be accessed from the WebOperationContext.

Because the WebFaultException derives from the regular FaultException, it has the advantage that it results in service implementations that can use SOAP based bindings or HTTP based bindings by just changing the web.config or app.config. Just like the regular FaultException, the WebFaultException exists in two versions: a non generic version and a generic version. The non generic version only supports setting the HTTP status code, while the generic version allows to specify a DataContract class that should be send to the client.

Demo WCF service

We will create a small sample WCF service to show usage of the WebFaultException class. The WCF service will always return a HTTP 401 status code along with some more detailed information about the error in JSON format.

The detail for the WebFaultException is very simple, it consists of 2 properties: a reason field and a detailed information field.

namespace JsonErrorMessage.Service
    public class ErrorData
        public ErrorData(string reason, string detailedInformation)
            Reason = reason;
            DetailedInformation = detailedInformation;

        public string Reason { get; private set; }

        public string DetailedInformation { get; private set; }

The interface of the service is very simple; it consist of only one method:

namespace JsonErrorMessage.Service
    public interface IHelloService
        [WebGet(ResponseFormat = WebMessageFormat.Json)]
        void Hello();

The implementation of the service is of course very simple:

namespace JsonErrorMessage.Service
    public class HelloService : IHelloService
        public void Hello()
            ErrorData errorData = new ErrorData("You are not allowed to access this service.",
                "We don't allow anybody to access this service.");

            throw new WebFaultException<ErrorData>(errorData,

And finally the following configuration needs to be added into the web.config or app.config file:

    <service name="JsonErrorMessage.Service.HelloService">
      <endpoint address=""
                contract="JsonErrorMessage.Service.IHelloService" />

Testing the demo WCF service

I will use Fiddler to test the implementation of the WCF service. Fiddler is a free tool to debug HTTP traffic and I highly recommend using it.

Fiddler has a request builder that allows creating HTTP requests very easily:

Fiddler Request Builder

When we execute this HTTP request we get the following result back:

Response Unauthorized JSon

The server returned a 401 error code and the body is our DataContract in JSON format. This is the result that we wanted.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}