.net - 如何使自定义 WCF 错误处理程序返回带有非 OK http 代码的 JSON 响应?

标签 .net wcf rest error-handling web-services

我正在使用 WCF 和 WebHttpBinding 实现 RESTful Web 服务。目前我正在研究错误处理逻辑,实现自定义错误处理程序(IErrorHandler);目的是让它捕获操作抛出的任何未捕获的异常,然后返回一个 JSON 错误对象(包括错误代码和错误消息 - 例如 { "errorCode": 123, "errorMessage": "bla"})返回给浏览器用户以及一个 HTTP 代码,例如 BadRequest、InteralServerError 或其他任何东西(实际上除了“OK”之外的任何东西)。这是我在错误处理程序的 ProvideFault 方法中使用的代码:

fault = Message.CreateMessage(version, "", errorObject, new DataContractJsonSerializer(typeof(ErrorMessage)));
var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);
var rmp = new HttpResponseMessageProperty();
rmp.StatusCode = System.Net.HttpStatusCode.InternalServerError;
rmp.Headers.Add(HttpRequestHeader.ContentType, "application/json");
fault.Properties.Add(HttpResponseMessageProperty.Name, rmp);

--> 这会返回 Content-Type: application/json,但是状态代码是“OK”而不是“InternalServerError”。
fault = Message.CreateMessage(version, "", errorObject, new DataContractJsonSerializer(typeof(ErrorMessage)));
var wbf = new WebBodyFormatMessageProperty(WebContentFormat.Json);
fault.Properties.Add(WebBodyFormatMessageProperty.Name, wbf);
var rmp = new HttpResponseMessageProperty();
rmp.StatusCode = System.Net.HttpStatusCode.InternalServerError;
//rmp.Headers.Add(HttpRequestHeader.ContentType, "application/json");
fault.Properties.Add(HttpResponseMessageProperty.Name, rmp);

--> 这会返回正确的状态代码,但是内容类型现在是 XML。
fault = Message.CreateMessage(version, "", errorObject, 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;

--> 这将返回正确的状态代码和正确的内容类型!问题是 http 正文现在有文本“无法加载源:http://localhost:7000/bla” ..' 而不是实际的 JSON 数据..

有任何想法吗?我正在考虑使用最后一种方法并将 JSON 粘贴在 HTTP StatusMessage header 字段而不是正文中,但这似乎不太好?

最佳答案

实际上,这对我有用。

这是我的 ErrorMessage 类:

    [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; }
    }

结合上面的最后一个片段:
        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; 

这给了我正确的 json 错误。谢谢。 :)

关于.net - 如何使自定义 WCF 错误处理程序返回带有非 OK http 代码的 JSON 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1149037/

相关文章:

.net - 从 .NET 数据库中检索数据的最快方法?

c# - 将消息从一个正在运行的控制台应用程序发送到另一个

wcf - perfmon.exe 中 WCF 计数器的含义

.net - WCF官方教程中的[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]是什么意思?

c# - WCF 服务应用程序配置

如果没有路径匹配,Java REST 有没有办法默认使用特定方法? (而不是得到 405)

.net - 如何首先使用 EF 4 代码将对象映射到 View ?

android - 带有 Android 的 Grape Rails API

json - Spring @ResponseBody 为原始类型生成无效的 JSON

c# - 如何针对双核、四核和更高的多处理器进行优化?