我在 C# 程序中遇到了一个问题,无法获取自定义 Fault Exception 的详细信息。当我在 Postman 中执行相同的调用时,可以正常获取响应结果,但是尝试在 C# 中执行相同的操作时,所有详细信息都返回 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
名称空间,但我似乎是错误的。