I\'m implementing a RESTful web service using WCF and the WebHttpBinding. Currently I\'m working on the error handling logic, implementing a custom error handler (IErrorHand
Actually, this works for me.
Here's my ErrorMessage class:
[DataContract]
public class ErrorMessage
{
public ErrorMessage(Exception error)
{
Message = error.Message;
StackTrace = error.StackTrace;
Exception = error.GetType().Name;
}
[DataMember(Name="stacktrace")]
public string StackTrace { get; set; }
[DataMember(Name = "message")]
public string Message { get; set; }
[DataMember(Name = "exception-name")]
public string Exception { get; set; }
}
Combined with the last snippet above:
fault = Message.CreateMessage(version, "", new ErrorMessage(error), new DataContractJsonSerializer(typeof(ErrorMessage)));
var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);
var response = WebOperationContext.Current.OutgoingResponse;
response.ContentType = "application/json";
response.StatusCode = HttpStatusCode.InternalServerError;
This gives me proper errors as json. Thanks. :)