我想写一篇
IErrorHandler
将处理的实现
AuthenticationException
实例(专有类型),然后在实现
ProvideFault
提供一个状态代码为403的传统HTTP响应作为故障消息。
到目前为止,我已经将我的第一个最佳猜测连接到了一个服务中,但是WCF似乎完全忽略了输出消息,即使正在调用错误处理程序。
目前,代码如下:
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添加的行为特征吗?或者是因为我所建立的信息是完全错误的!?我可以验证事件日志确实没有接收到任何内容。
我当前的测试环境是一个Webget方法(XML和JSON),托管在一个使用WebServiceHostfactory创建的服务中,并且ASP.NET兼容性已关闭。服务方法只是抛出有问题的异常。