FaultException<> Detail返回空值。

3

我在 C# 程序中遇到了一个问题,无法获取自定义 Fault Exception 的详细信息。当我在 Postman 中执行相同的调用时,可以正常获取响应结果,但是尝试在 C# 中执行相同的操作时,所有详细信息都返回 null。

Exception is null

以下是我的 C# 代码:

        try
        {
            Client.Open();
            var response = Client.findPerson(req);
            Client.Close();
        }            
        catch (FaultException<MIAPAPIException> e)
        {

            ErrorResponse error = new ErrorResponse
            {
                ErrorCode = e.Detail.ErrorCode,
                ErrorActor = e.Detail.ErrorActor,
                TimeStamp = e.Detail.ErrorTimestamp,
                ErrorDescription = e.Detail.Description
            };
        }

wsdl中定义的MIAPAPIException类如下:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://generic.url/exceptions")]
public partial class MIAPAPIException : object, System.ComponentModel.INotifyPropertyChanged {
    
    private string errorCodeField;
    
    private string errorActorField;
    
    private string descriptionField;
    
    private string furtherDetailsField;
    
    private string errorTimestampField;
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=0)]
    public string ErrorCode {
        get {
            return this.errorCodeField;
        }
        set {
            this.errorCodeField = value;
            this.RaisePropertyChanged("ErrorCode");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=1)]
    public string ErrorActor {
        get {
            return this.errorActorField;
        }
        set {
            this.errorActorField = value;
            this.RaisePropertyChanged("ErrorActor");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=2)]
    public string Description {
        get {
            return this.descriptionField;
        }
        set {
            this.descriptionField = value;
            this.RaisePropertyChanged("Description");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=3)]
    public string FurtherDetails {
        get {
            return this.furtherDetailsField;
        }
        set {
            this.furtherDetailsField = value;
            this.RaisePropertyChanged("FurtherDetails");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(IsNullable=true, Order=4)]
    public string ErrorTimestamp {
        get {
            return this.errorTimestampField;
        }
        set {
            this.errorTimestampField = value;
            this.RaisePropertyChanged("ErrorTimestamp");
        }
    }
    
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    
    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

我并没有发现任何问题。

如果我在C#中进行相同的请求,那么我在Postman中得到的响应如下:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
    <soapenv:Fault>
        <faultcode>soapenv:Server</faultcode>
        <faultstring>exceptions.MIAPAPIException</faultstring>
        <detail>
            <ns10:MIAPAPIException xmlns:ns10="http://generic.url/exceptions">
                <ErrorCode>CODE</ErrorCode>
                <ErrorActor>MSGValidator.validateSOAPMSG()</ErrorActor>
                <Description>DESC</Description>
                <FurtherDetails>DETAILS</FurtherDetails>
                <ErrorTimestamp>2022-10-11 14:34:16</ErrorTimestamp>
            </ns10:MIAPAPIException>
        </detail>
    </soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>

我不确定为什么我的 C# 代码在获取时发现此值为空。 Postman 中响应的顺序与 WSDL 的顺序以及两者之间的命名空间匹配相符。

我尝试更新 catch 语句为:

catch (FaultException<MIAPAPIException[]> e)

但这根本没有捕捉到错误。我认为这可能是一个数组,因为在详细信息的开头有<ns10:MIAPAPIException名称空间,但我似乎是错误的。


错误信息涉及代理。您可能需要在C#中禁用代理。通常最好的方法是使用类似Wireshark或Fiddler的嗅探器,比较工作和非工作情况。通常缺少标题或需要更改。看起来您正在使用HTTP,因此无需担心加密。可能是您的客户端站点需要HTTPS,这可能是问题所在。 - jdweng
调试XML序列化的最佳方法是将属性和类注释掉。即使在C#中缺少属性/类,您仍将接收剩余部分。然后取消注释代码,直到隔离出问题。目前我无法确定您是否收到了响应。因此请求可能有问题。 - jdweng
故障异常返回,但详细信息为空。我尝试注释自定义异常的属性,但无论包含什么,它仍然返回null。在Postman中进行相同的调用会返回带有填充详细信息的故障异常错误。 - SilentUK
2个回答

0

将.Close()替换为.Abort()。当代理处于故障状态时,应使用Abort。您可以尝试以下操作。

    if (client.State != CommunicationState.Faulted)
    {
        client.Close();
    }
    else
    {
        client.Abort();
    }

很遗憾,这不起作用。在我们触发Client.Abort()之前,FaultException已经返回了,只是异常的详细信息部分为空。在Postman中进行相同的调用会返回此错误,但详细信息已填充。 - SilentUK

0

请尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;

namespace ConsoleApplication49
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
            Envelope envelope = (Envelope)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
    public class Envelope
    {
        public Body Body { get; set; }
    }
    public class Body
    {
        public Fault Fault { get; set; }
    }
    public class Fault
    {
        [XmlElement(Namespace = "")]
        public string faultcode { get; set; }
        [XmlElement(Namespace = "")]
        public string faultstring { get; set; }
        [XmlArray(ElementName = "detail", Namespace = "")]
        [XmlArrayItem(ElementName = "MIAPAPIException", Namespace = "http://generic.url/exceptions")]
        public List<MIAPAPIException> MIAPAPIException { get; set; } 
    }
    public class MIAPAPIException
    {
        [XmlElement(Namespace = "")]
        public string ErrorCode { get; set; }
        [XmlElement(Namespace = "")]
        public string ErrorActor { get; set; }
        [XmlElement(Namespace = "")]
        public string Description { get; set; }
        [XmlElement(Namespace = "")]
        public string FurtherDetails { get; set; }

        DateTime _ErrorTimestamp { get; set; }
        [XmlElement(Namespace = "")]
        public string ErrorTimestamp { 
            get{ return  _ErrorTimestamp.ToString("yyyy-MM-dd HH:mm:ss");} 
            set{ _ErrorTimestamp = DateTime.ParseExact(value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) ;} 
        }
    }
}
 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接