gpt4 book ai didi

c# - 如何从IErrorHandler产生等效于HTTP 403的WCF消息?

转载 作者:行者123 更新时间:2023-12-03 07:55:54 24 4
gpt4 key购买 nike

我想编写一个处理AuthenticationException实例(专有类型)的IErrorHandler实现,然后在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兼容性。服务方法只是引发有问题的异常。

最佳答案

关于c# - 如何从IErrorHandler产生等效于HTTP 403的WCF消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2139847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com