【问题标题】:How to get Soap Fault details elements in ASP.NET Core app如何在 ASP.NET Core 应用程序中获取 Soap 故障详细信息元素
【发布时间】:2019-08-29 06:50:10
【问题描述】:

我尝试在我的 ASP.NET Core API 中引入来自 ASMX Websirvice 的 SOAP 异常。

旧版 ASMX Web 服务会抛出这样的 SOAP 异常:

<?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>

现在我正在尝试在我的 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;
   }
   ...
}

test var 仅包含错误代码“400”,仅此而已。 faultDetails ChildNode 为空。 如果我尝试调用GetDetail&lt;XmlNode&gt;(),则会出现序列化异常。 如何获取所有细节元素?

【问题讨论】:

    标签: c# asp.net-core exception soap soapfault


    【解决方案1】:

    好的,经过几次尝试,我找到了一个简单的解决方案。您只需在detail 节点中添加一个额外的节点。然后你会得到它作为一个带有 ChildNodes 的 XMLElement。

    你可以把它拿出来:

    var faultDetails = fault.GetDetail<XmlElement>();
    var errorCode = faultDetails.GetElementsByTagName("errorCode")[0]?.InnerText;
    var message = faultDetails.GetElementsByTagName("message")[0]?.InnerText;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多