0
votes

This answer explains how to get the text of a SOAP FaultException, but it only works when the contents are a serialised string, resulting in <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">details</string>.

This ONVIF device howevers uses the SOAP <env:Text> element, which can't be deserialised to a string.

How can I read the detaisl of the following 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>

This code is from the linked answer above:

} catch (FaultException ex) {
    System.ServiceModel.Channels.MessageFault mf = ex.CreateMessageFault();
    if (mf.HasDetail) {
        string detailedMessage = mf.GetDetail<string>();
        output.OutputText(detailedMessage);
    }
}

Which fails with:

An exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Runtime.Serialization.dll but was not handled in user code

Additional information: Expecting element 'string' from namespace 'http://schemas.microsoft.com/2003/10/Serialization/'.. Encountered 'Element' with name 'Text', namespace 'http://www.w3.org/2003/05/soap-envelope'.

There must be a built in deserialiser for it as the the <env:Reason> element uses the same node.

1
Digging through the Reference Source hasn't given any leadsDeanna

1 Answers

1
votes

You can get the detail as a "raw" XmlElement:

System.ServiceModel.Channels.MessageFault mf = ex.CreateMessageFault();
if (mf.HasDetail) {
    System.Xml.XmlElement detailedMessage = mf.GetDetail<System.Xml.XmlElement>();
    System.Diagnostics.Trace.WriteLine("Detail: " + detailedMessage.InnerText);
}

This should only be used when you know what the contents will be, unless wrapped in a try { } catch { } block