I have working WCF REST web service and can set status codes and status descriptions as usual:
OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
response.StatusCode = statusCode;
response.StatusDescription = detail.Error;
But I want to use WebFaultException. Unfortunately it alvays return {“Detail”:”Not Found”} when I run my code:
[Serializable]
[DataContract]
public class DtoError
{
public DtoError()
{
}
public DtoError(string error)
{
Error = error;
}
[DataMember]
public string Error { get; private set; }
}
var error = new DtoError(entityName + " is not existing");
throw new WebFaultException<DtoError>(error, HttpStatusCode.NotFound);
Can I return my custom error json object?
After investigation I found that instead of WebServiceHost we are using WebServiceHost2 from Rest Starter Kit. And inside that host we have to use WebProtocolException. So now my working code looks like that: