このページの推奨コードを使用しようとしています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);
}
}
WCFを使用して.NET 4.5でVS 2012を使用しています。アプリが 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 で作成した非同期 Web クローラーを使用して、名前空間の URL の一部が解決できないことがわかりました。エラーのあるものを修正してから、クローラーを再度実行して名前空間ページの長さを合計しました。合計は 715,965 バイトで、XmlDictionaryReaderQuotas のすべてのクォータの Int32.MaxValue よりもはるかに小さくなっています。おそらく、XmlDictionaryReader にバグがあるか、それが返すエラーが本当の問題ではないでしょうか?
最終的に、SOAP-ENV:Body で実際には使用されなかった名前空間定義を削除することで、メッセージの作成が機能するようになりました (つまり、Subcode 要素で使用される xmlns:ter のみを保持します)。しかしもちろん、サービスが SOAP エラーを生成しているため、これで問題が実際に解決されるわけではありません。サービスの実装を変更することはできません (これはサードパーティのデバイスです - Onvif カメラです)。
さらに、クォータをこれ以上大きくすることはできません。では、この例外を他にどのように処理するのでしょうか?