这个答案解释了如何获取 SOAP 的文本FaultException
,但它仅在内容是序列化字符串时才有效,导致<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">details</string>
.
然而,此 ONVIF 设备使用 SOAP<env:Text>
元素,该元素无法反序列化为字符串。
如何阅读以下内容的详细信息FaultException
?
<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns: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:rpc="http://www.w3.org/2003/05/soap-rpc" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:ter="http://www.onvif.org/ver10/error" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:Action>http://www.w3.org/2005/08/addressing/soap/fault</wsa:Action>
</env:Header>
<env:Body>
<env:Fault>
<env:Code>
<env:Value>env:Receiver</env:Value>
<env:Subcode>
<env:Value>ter:Action</env:Value>
</env:Subcode>
</env:Code>
<env:Reason>
<env:Text xml:lang="en">ActionFailed</env:Text>
</env:Reason>
<env:Detail>
<env:Text>It is not possible to operate because of the unsupported video source mode.</env:Text>
</env:Detail>
</env:Fault>
</env:Body>
</env:Envelope>
此代码来自上面的链接答案:
} catch (FaultException ex) {
System.ServiceModel.Channels.MessageFault mf = ex.CreateMessageFault();
if (mf.HasDetail) {
string detailedMessage = mf.GetDetail<string>();
output.OutputText(detailedMessage);
}
}
失败了:
System.Runtime.Serialization.dll 中出现“System.Runtime.Serialization.SerializationException”类型的异常,但未在用户代码中处理
附加信息:来自命名空间“ http://schemas.microsoft.com/2003/10/Serialization/ ”的预期元素“字符串”。遇到名称为“文本”、命名空间“ http://www.w3”的“元素”。 org/2003/05/soap-信封'。
必须有一个内置的反序列化器,因为<env:Reason>
元素使用相同的节点。