处理SOAP FaultException

时间:2015-06-11 07:53:31

标签: c# soap

我在捕捉SOAP FaultException时遇到问题。

服务器响应是:

<soap-env:Fault xmlns:soap-env = "http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://schemas.xmlsoap.org/soap/envelope/">
     <faultcode>soap-env:Client</faultcode>
     <faultstring>Test</faultstring>
     <detail>
            <brin:FaultResponse>
           <cdm:SingleError>
        <cdm:ErrorCode>-1</cdm:ErrorCode>
        <cdm:ErrorDetails>Custom detail</cdm:ErrorDetails>
          </cdm:SingleError>
          </brin:FaultResponse>
     </detail>
</soap-env:Fault>

在.NET代码中,我有以下try catch块:

try
{
   //call client
}
catch(FaultException faultException)
{

}
catch (Exception ex)
{
      //Always get here - with type of Exception == CommunicationException
}

1 个答案:

答案 0 :(得分:0)

您可能需要:

catch (FaultException faultex) {
    var msgFault = faultex.CreateMessageFault();

    if (msgFault.HasDetail) {
        var detailNode = msgFault.GetDetail<XmlElement>();
        //detailNode is where you will find what you were looking for
    }
}

感谢 https://social.msdn.microsoft.com/Forums/vstudio/en-US/3350a1a6-2dff-4031-b113-93e0e5391a65/how-to-get-the-detail-of-a-soap-fault-