gpt4 book ai didi

c# - 解析 WCF ProtocolException 时出现 System.ServiceModel.CommunicationException

转载 作者:行者123 更新时间:2023-11-30 17:48:30 26 4
gpt4 key购买 nike

我正在尝试使用此页面中推荐的代码 http://blogs.msdn.com/b/nathana/archive/2011/03/31/deciphering-a-soap-fault-with-a-400-status-code.aspx如下:

static FaultException ParseProtocolExecption(ProtocolException ex)
{
try
{
System.IO.Stream stream = (ex.InnerException as WebException).Response.GetResponseStream();
System.Xml.XmlReader xmr = System.Xml.XmlReader.Create(stream);
Message message = Message.CreateMessage(xmr, (int)stream.Length, MessageVersion.Soap12);
MessageFault mf = MessageFault.CreateFault(message, (int)stream.Length);
FaultException fe = new FaultException(mf);
message.Close();
return fe;
}
catch (Exception)
{
return new FaultException(ex.Message);
}
}

我正在使用 VS 2012 和 .NET 4.5,使用 WCF。当应用收到 400 Bad Request 并将 ProtocolException 传递给 ParseProtocolException 时,它会在此行抛出异常:

Message message = Message.CreateMessage(xmr, (int)stream.Length, MessageVersion.Soap12);

出现 System.ServiceModel.CommunicationException:“缓冲 XML 内容所需的大小超出了缓冲配额。”

stream.Length = 2,704 字节,不是很大。我尝试了本网站建议的解决方案 http://blogs.msdn.com/b/drnick/archive/2007/08/07/increasing-the-maximum-fault-size.aspx .但是,即使 MaxFaultSize = 1 Mb,它也会出现相同的错误。

代替这一行:

System.Xml.XmlReader xmr = System.Xml.XmlReader.Create(stream);

我已经试过了:

xmr = XmlDictionaryReader.CreateTextReader(stream, XmlDictionaryReaderQuotas.Max);

将所有配额设置为其最大值 (Int32.MaxValue);但是,我在调用 CreateMessage 时仍然遇到同样的错误。

来自 System.Net.WebException 的示例响应流如下:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa5="http://www.w3.org/2005/08/addressing" xmlns:c14n="http://www.w3.org/2001/10/xml-exc-c14n#" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:xmime5="http://www.w3.org/2005/05/xmlmime" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:tptz="http://www.onvif.org/ver20/ptz/wsdl" xmlns:tds2="http://www.onvif.org/ver10/schema" xmlns:tds3="http://docs.oasis-open.org/wsn/b-2" xmlns:tds4="http://docs.oasis-open.org/wsrf/bf-2" xmlns:tds5="http://docs.oasis-open.org/wsn/t-1" xmlns:timg="http://www.onvif.org/ver20/imaging/wsdl" xmlns:trt="http://www.onvif.org/ver10/media/wsdl" xmlns:tan="http://www.onvif.org/ver20/analytics/wsdl" xmlns:tds6="http://www.canon.com/ns/networkcamera/onvif/va/schema" xmlns:tmd="http://www.onvif.org/ver10/deviceIO/wsdl" xmlns:tev="http://www.onvif.org/ver10/events/wsdl" xmlns:tds9="http://docs.oasis-open.org/wsrf/r-2" xmlns:tds="http://www.onvif.org/ver10/device/wsdl" xmlns:ter="http://www.onvif.org/ver10/error">
<SOAP-ENV:Header></SOAP-ENV:Header>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<SOAP-ENV:Code>
<SOAP-ENV:Value>SOAP-ENV:Sender</SOAP-ENV:Value>
<SOAP-ENV:Subcode>
<SOAP-ENV:Value>ter:NotAuthorized</SOAP-ENV:Value>
</SOAP-ENV:Subcode>
</SOAP-ENV:Code>
<SOAP-ENV:Reason>
<SOAP-ENV:Text xml:lang="en">Sender Not Authorized</SOAP-ENV:Text>
</SOAP-ENV:Reason>
<SOAP-ENV:Node>http://www.w3.org/2003/05/soap-envelope/node/ultimateReceiver</SOAP-ENV:Node>
<SOAP-ENV:Role>http://www.w3.org/2003/05/soap-envelope/role/ultimateReceiver</SOAP-ENV:Role>
<SOAP-ENV:Detail>The action requested requires authorization and the sender is not authorized</SOAP-ENV:Detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

使用我在 F# Interactive 中编写的异步网络爬虫,我发现一些命名空间 url 不可解析。我更正了错误的部分,然后再次运行爬虫以计算命名空间页面的长度。总数为 715,965 字节,远小于 XmlDictionaryReaderQuotas 中所有配额的 Int32.MaxValue。也许 XmlDictionaryReader 有错误,或者它返回的错误不是真正的问题?

我最终通过删除 SOAP-ENV:Body 中未实际使用的命名空间定义(即仅保留 Subcode 元素中使用的 xmlns:ter)来创建消息。但是,当然,这并没有真正解决问题,因为服务正在生成 SOAP 错误;而且我无法更改服务实现(它是第 3 方设备 - Onvif 相机)。

此外,我无法再增加配额;那么,还有什么方法可以处理这个异常呢?

最佳答案

解决方案是捕获 CommunicationException,然后在不需要命名空间解析的流中对 XML 进行替代解析:

    public static FaultException ParseProtocolException(System.ServiceModel.ProtocolException ex) {
var stream = (ex.InnerException as System.Net.WebException).Response.GetResponseStream();
try {
var xmr = XmlReader.Create(stream);
var message = Message.CreateMessage(xmr, (int)stream.Length, MessageVersion.Soap12);
var mf = MessageFault.CreateFault(message, (int)stream.Length);
message.Close();
return new FaultException(mf);
} catch (CommunicationException) { // If CreateMessage has a problem parsing the XML,
// then this error will be thrown. Most likely, there is an unresolvable namespace reference.
// Do an alternate parse
stream.Seek(0, System.IO.SeekOrigin.Begin);
var soapFault = GetSoapFault(stream);
return new FaultException(soapFault.Reason);
}
}

catch 将流重置为开头,然后使用以下内容通过 XmlReader 获取 SOAP 错误的具体信息:

    private struct SoapFault {
public string Subcode;
public string Reason;
public string Detail;
}
private static string GetTextChild(XmlReader xmr, string childName) {
return xmr.ReadToDescendant(childName) ?
xmr.ReadString() : System.String.Empty;
}
private static SoapFault GetSoapFault(System.IO.Stream s) {
var xr = XmlReader.Create(s);
var fault = new SoapFault();
if (xr.ReadToFollowing("SOAP-ENV:Subcode")) {
fault.Subcode = GetTextChild(xr, "SOAP-ENV:Value");
if (xr.ReadToFollowing("SOAP-ENV:Reason")) {
fault.Reason = GetTextChild(xr, "SOAP-ENV:Text");
if (xr.ReadToFollowing("SOAP-ENV:Detail"))
fault.Detail = GetTextChild(xr, "SOAP-ENV:Text");
}
}
return fault;
}

关于c# - 解析 WCF ProtocolException 时出现 System.ServiceModel.CommunicationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22998377/

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