如何从IErrorHandler生成HTTP 403等效的WCF消息?

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

到目前为止,我有一个最好的猜测连接到服务,但WCF似乎完全忽略输出消息,即使正在调用error handling程序。

目前,代码如下所示:

 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 } 

有了这个,我只得到标准的WCF html’服务器遇到处理请求的错误。 有关详细信息,请参阅服务器日志。 – 如果没有IErrorHandler会发生什么。 这是WebServiceHost添加的行为的一个特征吗? 或者是因为我正在构建的消息是完全错误的!? 我可以validation事件日志确实没有收到任何内容。

我当前的测试环境是一个WebGet方法(包括XML和Json),它托管在使用WebServiceHostFactory创建的服务中,并关闭了Asp.Net兼容性。 服务方法只会抛出有问题的exception。

试试这个: 从支持AJAX的WCF服务返回错误详细信息

还有这个

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