首页 > 解决方案 > 从故障异常中获取详细信息不显示完整的详细信息

问题描述

我有以下格式的 xml 文件:

<?xml version="1.0" encoding="utf-16"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
  <soap:Body>
    <soap:Fault>
      <soap:Code>
        <soap:Value>soap:Receiver</soap:Value>
      </soap:Code>
      <soap:Reason>
        <soap:Text xml:lang="en">Cannot create order</soap:Text>
      </soap:Reason>
      <soap:Detail>
        <ComplianceViolationInfos xmlns="http://aaim.sungard.com/order/service/v1_0" xmlns:ns10="http://aaim.sungard.com/issuer/schema/v2011_06_01" xmlns:ns2="http://aaim.sungard.com/datatypes/schema/v2011_06_01" xmlns:ns3="http://aaim.sungard.com/order/schema/v2011_06_01" xmlns:ns4="http://aaim.sungard.com/fxorder/schema/v2011_06_01" xmlns:ns5="http://aaim.sungard.com/portfolio/schema/v2011_06_01" xmlns:ns6="http://aaim.sungard.com/transaction/schema/v2011_06_01" xmlns:ns7="http://aaim.sungard.com/compliance/schema/v2011_06_01" xmlns:ns8="http://aaim.sungard.com/user/schema/v2011_06_01" xmlns:ns9="http://aaim.sungard.com/instrument/schema/v2011_06_01">
          <ns7:violationInfo>
            <ns7:RuleType>REQ_OVERRIDE</ns7:RuleType>
            <ns7:RuleName> Name </ns7:RuleName>
            <ns7:Diagnostic> data </ns7:Diagnostic>
          </ns7:violationInfo>
          <ns7:violationInfo>
            <ns7:RuleType> rule </ns7:RuleType>
            <ns7:RuleName> data </ns7:RuleName>
            <ns7:Diagnostic>1. data </ns7:Diagnostic>
          </ns7:violationInfo>
          <ns7:violationInfo>
            <ns7:RuleType>WARNING</ns7:RuleType>
            <ns7:RuleName> name </ns7:RuleName>
            <ns7:Diagnostic> data </ns7:Diagnostic>
          </ns7:violationInfo>
        </ComplianceViolationInfos>
      </soap:Detail>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

在发生故障的情况下,我想访问位于 details 标志之间的所有数据,进一步处理它,然后将其返回给用户。我正在开发一个 .NET Core 应用程序,因此我不能使用 SoapExceptions。为了实现这一点,我的代码中有一个 try-catch-blog,它捕获了一个故障异常:

 try
        {
            //..//
        }
        catch (FaultException e)
        {
            MessageFault messagefault = e.CreateMessageFault();
            var xmlElement = messagefault.GetReaderAtDetailContents();
            string test = xmlElement.ReadContentAsString();
            throw e;
        }

上面的代码不返回详细标志之间的数据。我也已经尝试过:

string xmlElement = messagefault.GetDetail<XmlElement>().Value

这返回了一个空字符串。

我认为原因是我无法弄清楚该服务的 wcf 中的错误。序列化的类是:

[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://aaim.sungard.com/compliance/schema/v2011_06_01")]
public partial class ComplianceViolationInfo
{
     private ViolationInfo[] ViolationInfoField;

     [System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
     [System.Xml.Serialization.XmlArrayItemAttribute("violationInfo", IsNullable = false)]
     public ViolationInfo[] ViolationInfo
     {
         get
         {
             return this.ViolationInfoField;
         }
         set
         {
             this.ViolationInfoField = value;
         }
     } 

}

[System.CodeDom.Compiler.GeneratedCodeAttribute("dotnet-svcutil", "1.0.0.1")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://aaim.sungard.com/compliance/schema/v2011_06_01")]
public partial class ViolationInfo
{
    private string ruleTypeField;

    private string ruleNameField;

    private string diagnosticField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
    public string RuleType
    {
        get
        {
            return this.ruleTypeField;
        }
        set
        {
            this.ruleTypeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
    public string RuleName
    {
        get
        {
            return this.ruleNameField;
        }
        set
        {
            this.ruleNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order = 2)]
    public string Diagnostic
    {
        get
        {
            return this.diagnosticField;
        }
        set
        {
            this.diagnosticField = value;
        }
    }
}

它是这样调用的:

[System.ServiceModel.OperationContractAttribute(Action="", ReplyAction="*")]
    [System.ServiceModel.FaultContractAttribute(typeof(OrderService.ComplianceViolationInfo), Action="", Name="ComplianceViolationInfos", Namespace="http://aaim.sungard.com/order/service/v1_0")]
    [System.ServiceModel.FaultContractAttribute(typeof(OrderService.FaultInfo), Action = "", Name = "FaultInfo", Namespace = "http://aaim.sungard.com/order/service/v1_0")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    System.Threading.Tasks.Task<OrderService.CreateOrderResponse1> CreateOrderAsync(OrderService.CreateOrderRequest request);

标签: c#.netxmlwcffaultexception

解决方案


推荐阅读