user1396149
user1396149

Reputation: 115

Customize WCF Rest error responses

I have written a WCF REST API for third-party use. One of the things I want to do is to return custom error responses to clients if anything goes wrong. I don't want the WCF default error page showing that internal server error has occurred or method name not found.

To do so I throw WebFaultException<Error> where necessary. This return the following type of response to the client:

<Error>
  <type>MissingTag</type>
  <Desc>Tag 349 is missing</Desc>
</Error>

But how can I handle if any other type of error occurs like a serialization error or the "Method not found" error or place where I want to check that POST, PUT and PATCH have http header content-type present. I want to throw WebFaultException<> there too. I tried looking into IErrorHandler but could not get it working.

Any one got ideas on how to implement this type of thing. Also can I have a simple code demonstrating the IErrorHandler usage?

Upvotes: 1

Views: 1413

Answers (1)

Rajesh
Rajesh

Reputation: 7876

You can look into Message Inspectors BeforeSendReply for customizing the reply that needs to be sent to the client

If the content-type is not set when the request is made you can look into the AfterReceiveRequest where in you can customise the request received and then action as needed.

Upvotes: 1

Related Questions