导致在WCF中返回HTTP错误状态 [英] Causing HTTP error status to be returned in WCF

查看:361
本文介绍了导致在WCF中返回HTTP错误状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让我的WCF服务以RESTful方式传递错误?具体来说,如果调用者将无效的查询字符串参数传递给我的方法,我希望将400或404 HTTP错误返回给用户。当我搜索与WCF相关的HTTP错误状态时,我所能找到的只是人们试图解决他们收到的错误的页面。我宁愿不只是抛出 FaultException ,因为它会转换为500错误,这不是正确的状态代码。

How can I get my WCF service to communicate errors in a RESTful manner? Specifically, if the caller passes invalid query string parameters to my method, I'd like to have a 400 or 404 HTTP error returned to the user. When I search for HTTP error status in relation to WCF, all I can find are pages where people are trying to resolve errors they're receiving. I'd rather not just throw a FaultException, because that gets converted to a 500 error, which is not the correct status code.

推荐答案

我在这里找到了一篇有用的文章: http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/ 。基于此,这就是我提出的:

I found a helpful article here: http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/. Based on that, this is what I came up with:

public class HttpErrorsAttribute : Attribute, IEndpointBehavior
{
    public void AddBindingParameters(
        ServiceEndpoint endpoint, 
        BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(
        ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(
        ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        var handlers = endpointDispatcher.ChannelDispatcher.ErrorHandlers;
        handlers.Clear();
        handlers.Add(new HttpErrorHandler());
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }

    public class HttpErrorHandler : IErrorHandler
    {
        public bool HandleError(Exception error)
        {
            return true;
        }

        public void ProvideFault(
            Exception error, MessageVersion version, ref Message fault)
        {
            HttpStatusCode status;
            if (error is HttpException)
            {
                var httpError = error as HttpException;
                status = (HttpStatusCode)httpError.GetHttpCode();
            }
            else if (error is ArgumentException)
            {
                status = HttpStatusCode.BadRequest;
            }
            else
            {
                status = HttpStatusCode.InternalServerError;
            }

            // return custom error code.
            fault = Message.CreateMessage(version, "", error.Message);
            fault.Properties.Add(
                HttpResponseMessageProperty.Name,
                new HttpResponseMessageProperty
                {
                    StatusCode = status,
                    StatusDescription = error.Message
                }
            );
        }
    }
}

这允许我添加一个我的服务的 [HttpErrors] 属性。在我的自定义错误处理程序中,我可以确保发送我要发送的HTTP状态代码。

This allows me to add a [HttpErrors] attribute to my service. In my custom error handler, I can ensure that the HTTP status codes I'd like to send are sent.

这篇关于导致在WCF中返回HTTP错误状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆