分析WCF协议时出现System.ServiceModel.CommunicationException

本文关键字:System ServiceModel CommunicationException WCF 协议 分析 | 更新日期: 2023-09-27 17:57:58

我正在尝试使用此页面中推荐的代码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错误的请求,并将ProtocolException传递给ParseProtocolException时,它会在以下行抛出一个异常:

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

带有System.ServiceModel.CommunicationException:"缓冲XML内容所需的大小超过了缓冲区配额。"

小溪。长度=2704字节,不是很大。我尝试了这个网站上建议的解决方案http://blogs.msdn.com/b/drnick/archive/2007/08/07/increasing-the-maximum-fault-size.aspx.然而,即使MaxFaultSize=1Mb,它也会得到相同的错误。

代替这行:

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是不可解析的。我纠正了错误的部分,然后再次运行爬网程序来计算名称空间页面的长度。总数为715965字节,远小于XmlDictionaryReaderQuotas中所有配额的Int32.MaxValue。也许XmlDictionaryReader有错误,或者它返回的错误不是真正的问题?

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

更重要的是,我不能再增加配额了;那么,如何处理这个异常呢?

分析WCF协议时出现System.ServiceModel.CommunicationException

解决方案是捕获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;
    }