【问题标题】:Causing HTTP error status to be returned in WCF导致在 WCF 中返回 HTTP 错误状态
【发布时间】:2011-03-15 16:15:19
【问题描述】:

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

【问题讨论】:

    标签: wcf http rest http-status-codes


    【解决方案1】:

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

    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 状态代码。

    【讨论】:

      【解决方案2】:

      如果您使用的是标准 WCF,那么FaultException 是解决此问题的正确方法。如果你不想这样做并且你想成为 RESTful,那么你应该使用REST WCF approachHere is a quick start template for 4.03.5)。这完全支持向客户端返回 HTTP 状态代码。

      【讨论】:

      • 很好的链接。不幸的是,我不是在寻找创建新服务的模板,而是一种添加到现有服务的方法。
      【解决方案3】:

      我想实现您所要求的相同解决方案,当您想使用 HTTP 状态代码时,下面的链接非常适合。

      How can I return a custom HTTP status code from a WCF REST method?

      您可以访问一个WebOperationContext,它有一个OutgoingWebResponseContext 类型的OutgoingResponse 属性,其中有一个可以设置的StatusCode 属性。

      WebOperationContext ctx = WebOperationContext.Current;
      ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 2016-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-24
        相关资源
        最近更新 更多