如何从IErrorHandler生成HTTP 403等效的WCF消息? [英] How to produce an HTTP 403-equivalent WCF Message from an IErrorHandler?

查看:113
本文介绍了如何从IErrorHandler生成HTTP 403等效的WCF消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个 IErrorHandler 实现,将处理 AuthenticationException 实例(一种专有类型),然后在执行 ProvideFault 中提供一个传统的Http响应状态码为403作为故障消息。



到目前为止,我有第一个最好的猜测是连接到一个服务中,但WCF似乎完全忽略了输出消息,即使正在调用错误处理程序。 p>

目前,代码如下所示:

  public class AuthWeb403ErrorHandler :IErrorHandler 
{
#region IErrorHandler成员
public bool HandleError(异常错误)
{
返回错误是AuthenticationException;
}

public void ProvideFault(异常错误,MessageVersion版本,ref消息错误)
{
//第一次尝试 - 只是一个刺在黑暗中,真的
HttpResponseMessageProperty属性= new HttpResponseMessageProperty();
property.SuppressEntityBody = true;
property.StatusCode = System.Net.HttpStatusCode.Forbidden;
property.StatusDescription =禁止;

var m = Message.CreateMessage(version,null);

m.Properties [HttpResponseMessageProperty.Name] = property;
fault = m;
}
#endregion
}

我只是得到标准的WCF html'服务器遇到处理请求的错误。有关更多详细信息,请参阅服务器日志。 - 如果没有IErrorHandler会发生什么。这是WebServiceHost添加行为的一个特征吗?或者是因为我正在建设的消息是错误的!我可以验证事件日志确实没有收到任何东西。



我目前的测试环境是一个WebGet方法(包括XML和Json) WebServiceHostFactory和Asp.Net兼容性关闭。

解决方案

尝试这个:
从启用AJAX的WCF服务中获取错误详细信息



这个



http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/


I want to write an IErrorHandler implementation that will handle AuthenticationException instances (a proprietary type), and then in the implementation of ProvideFault provide a traditional Http Response with a status code of 403 as the fault message.

So far I have my first best guess wired into a service, but WCF appears to be ignoring the output message completely, even though the error handler is being called.

At the moment, the code looks like this:

public class AuthWeb403ErrorHandler : IErrorHandler
{
  #region IErrorHandler Members
  public bool HandleError(Exception error)
  {
    return error is AuthenticationException;
  }

  public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
  {
    //first attempt - just a stab in the dark, really
    HttpResponseMessageProperty property = new HttpResponseMessageProperty();
    property.SuppressEntityBody = true;
    property.StatusCode = System.Net.HttpStatusCode.Forbidden;
    property.StatusDescription = "Forbidden";

    var m = Message.CreateMessage(version, null);

    m.Properties[HttpResponseMessageProperty.Name] = property;
    fault = m;
  }
  #endregion
}

With this in place, I just get the standard WCF html 'The server encountered an error processing the request. See server logs for more details.' - which is what would happen if there was no IErrorHandler. Is this a feature of the behaviours added by WebServiceHost? Or is it because the message I'm building is simply wrong!? I can verify that the event log is indeed not receiving anything.

My current test environment is a WebGet method (both XML and Json) hosted in a service that is created with the WebServiceHostFactory, and Asp.Net compatibility switched off. The service method simply throws the exception in question.

解决方案

try this: Returning Error Details from AJAX-Enabled WCF Service

and this

http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/

这篇关于如何从IErrorHandler生成HTTP 403等效的WCF消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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