Search code examples
c#wcfasmxfaultexceptionsoapfault

WCF Client Catching SoapException from asmx webservice


I have a WCF service that calls an asmx web service. That web service throws an enxception that looks like this:

        <soap:Body>
        <soap:Fault>
            <faultcode>soap:Server</faultcode>
            <faultstring>System.Web.Services.Protocols.SoapException:  error
                         service.method()</faultstring>
            <faultactor>https://WEBADDRESS</faultactor>
            <detail>
                <message>Invalid ID</message>
                <code>00</code>
            </detail>
        </soap:Fault>
    </soap:Body>

In c# I can catch it as a FaultException however it has no details property. How can I get at the details of this Exception?


Solution

  • After playing around with this for a long time I found that off of the FaultException object you can create a MessageFault. MessageFault has a property HasDetail that indicates if the detail object is present. From there you can grab the Detail object as an XmlElement and get its value. The following catch block works well.

     catch (System.ServiceModel.FaultException FaultEx)
      {
       //Gets the Detail Element in the
       string ErrorMessage;
       System.ServiceModel.Channels.MessageFault mfault = FaultEx.CreateMessageFault();
       if (mfault.HasDetail)
         ErrorMessage = mfault.GetDetail<System.Xml.XmlElement>().InnerText;
      } 
    

    This yields "Invalid ID." from the sample fault in the question.