Search code examples
c#wcf.net-4.0wcf-rest

Create custom WebFaultException


In a REST WCF Service, is there a way to create a custom WebFaultException?

Right now I am using WebFaultException, but the response doesn't match what the client is expecting. So I'd like to create a custom WebFaultException which will set the HttpStatusCode and add a simple message in the response body (based on my own structure).


Solution

  • I recommed avoiding changing HtpStatusCode from inside the custom FaultException. Extending the sevice EndPoint behavior is more robust, because it operates at the EndpointDispatcher level, and allows you to inspect and modify HttpStatusCode and Message body before sending back the reply, as well as adding custom Message properties in the reply. You will need to add the behaviorExtensions in your service web.config file of course.

    You can use the following article as a guideline (eventhough it focuses on handling declared fault exceptions for silverlight clients, but the premise is similar)

    Creating and Handling Faults in Silverlight