0

The response of the SOAP request is the following xml output.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
    <SOAP-ENV:Fault>
        <faultcode>SOAP-ENV:Client</faultcode>
        <faultstring xml:lang="en">INVALID_REQUEST - Request is not a valid for XSD rules.</faultstring>
        <detail>
            <ServiceError xmlns="http://test.test.com/xmlschema/common">
                <code>INVALID_REQUEST</code>
                <description>Request is not a valid for XSD rules.</description>
                <details>cvc-datatype-valid.1.2.1: '?' is not a valid value for 'integer'.</details>
                <details>cvc-type.3.1.3: The value '?' of element 'pos:userId' is not valid.</details>
            </ServiceError>
        </detail>
    </SOAP-ENV:Fault>
</SOAP-ENV:Body></SOAP-ENV:Envelope>

While trying to catch this error with FaultException, I created the ServiceError class as below and gave it as TDetail to FaultException. However, I could not catch the details of this error.It doesn't come inside the catch block at all. Where am I going wrong or is there a method you recommend?

        try{
           //here is the code where i got the error
        }
        catch (FaultException<ServiceError> e)
        {
        }
    
        [DataContract]
        public class ServiceError
        {
            [DataMember]
            public string code { get; set; }
            [DataMember]
            public string description { get; set; }
            [DataMember]
            public string[] details { get; set; }
        }

1 Answers1

0

You can try the following method, The detail node of the message failure should contain XML.
GetDetail will deserialize this XML into the given object.For details you can check this post.
The content is a string, the code:

catch (FaultException ex)
{
    MessageFault msgFault = ex.CreateMessageFault();
   var msg = msgFault.GetReaderAtDetailContents().Value;
     //throw Detail
}

If the details are not strings, the code:

MessageFault msgFault = ex.CreateMessageFault();
XmlReader readerAtDetailContents = msgFault.GetReaderAtDetailContents();
var readOuterXml = readerAtDetailContents.ReadOuterXml(); 
var data = XElement.Parse(readOuterXml);
Dictionary<string, string> element = data.Elements().ToDictionary(elementKey => elementKey.Name.LocalName, elementVal => elementVal.Value, null);
Lan Huang
  • 613
  • 2
  • 5