Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Soap Fault details elements in ASP.NET Core app

I try to cath a SOAP exception from ASMX Websirvice in my ASP.NET Core API.

The legacy ASMX Webservice throw a SOAP Exception like this:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <soap:Fault>
            <faultcode>soap:Client</faultcode>
            <faultstring>System.Web.Services.Protocols.SoapException: An order must be specified
   at legacyNamespace.Common.WebServiceBase.CheckUserPermissionsForProject(String projectId)
   at legacyNamespace.CustomerApp.Defect.Defect.AcceptDefect(AcceptDefectModel defect)</faultstring>
            <detail>
                <errorCode>400</errorCode>
                <message>An order must be specified</message>
            </detail>
        </soap:Fault>
    </soap:Body>
</soap:Envelope>

Now I'm trying to catch and process this exception in my ASP.NET Core 2.2 Web API:

try
{
   // call the asmx service
}
catch (FaultException ex)
{
   var fault = ex.CreateMessageFault();
   if (fault.HasDetail)
   {
      var faultDetails = fault.GetDetail<XmlElement>();
      var test = faultDetails.InnerText;
   }
   ...
}

And the test var contains only the errorCode "400" and nothing more. faultDetails ChildNodes are empty. If I try to call GetDetail<XmlNode>() than I got a serialization exception. How can I get all detail elements?

like image 210
Poiser Avatar asked Feb 03 '26 10:02

Poiser


1 Answers

Ok, after a few tries, I found a simple solution. You just need to add an additional node into the detail node. And then you will get it as an XMLElement with ChildNodes.

Than you can get it out:

var faultDetails = fault.GetDetail<XmlElement>();
var errorCode = faultDetails.GetElementsByTagName("errorCode")[0]?.InnerText;
var message = faultDetails.GetElementsByTagName("message")[0]?.InnerText;
like image 93
Poiser Avatar answered Feb 05 '26 01:02

Poiser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!