19Nov/090
Return a custom HTTP status code with a WCF service
We have a situation where we need to return a specific HTTP status code during some situations.
At first I thought I would have to use custom a FaultException. This looked promissing, but it was not required.
Then I thought perhaps I could use a fault Messages with the HttpResponseMessageProperty.
Then I foudn the easiest way was to set the WebOperationContext's OutgoingResponse Status Code.
This was very simple.
private XElement ProcessQuestion(InputXMLObject inputXMLObject)
{
...
if (errorCode == "1.134")
{
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.SeeOther;
return new XElement("Error");
}
...
}
Setting the OutgoingResponse is all that is necessary to set the HTTP status code.
Very simple?