Multiple name spaces in a soap fault message causing FaultException deserialization to fail

后端 未结 3 787
面向向阳花
面向向阳花 2021-01-22 20:46

We\'re connecting to a web service and the fault message we\'re getting back isn\'t deserializing (at all), and no version of class that I can make will deserialize correctly. W

3条回答
  •  情话喂你
    2021-01-22 21:42

    What we ended up doing was to give up trying to catch the Fault and pass it back in the SOAP channel. Instead, we created a custom exception, and wired up a MessageInspector to watch for the faults and throw it as an exception.

    The relevant part of the (sanitized) code:

       public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                XmlDictionaryReader xdr = reply.GetReaderAtBodyContents();
                XNode xn = XDocument.ReadFrom(xdr);
                string s = xn.ToString();
                XDocument xd = XDocument.Parse(s);
                XNamespace nsSoap = "http://schemas.xmlsoap.org/soap/envelope/";
                XNamespace ns = "http://eGov.gov";
                XElement xErrorClass = xd.Element(nsSoap + "Fault").Element("detail").Element(ns + "eGov2Exception").Element(ns + "RequestErrorClassification");
                XElement xErrorCode = xd.Element(nsSoap + "Fault").Element("detail").Element(ns + "eGov2Exception").Element(ns + "RequestErrorCode");
                XElement xErrorMessage = xd.Element(nsSoap + "Fault").Element("detail").Element(ns + "eGov2Exception").Element(ns + "RequestErrorMessage");
    
                throw new eGovException(xErrorClass.Value, xErrorCode.Value, xErrorMessage.Value);
            }
        }
    

    The main application then uses:

    catch (eGovException ex)
    {
    // Handles exception here.
    }

    Much too much time was wasted trying to correct name spaces. Thanks for answering.

提交回复
热议问题