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

浪尽此生 提交于 2019-12-02 01:15:59

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.

One way to handle this if serialization is failing completely is to use a OoperationContract using the Message type as input and output. This way you can manually parse the XML when Iffault == true or use GetBody() to get the regular contents if no error occurred.

First of all, if you cannot get the WSDL from the people who produced this web service, then they have no business having a SOAP-based web service. A proper WSDL would solve your problem, whether or not "?WSDL" is used.

Second, please post the code of the eGov2ExceptionType class. I suspect it does not have the http://eGov.gov namespace set on it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!