在C#中访问SoapAPI我将WSDL文件作为SeriviceRefrence添加到我的c# classLinrery中,这说明我无法捕获该classLinrery抛出的实际异常
我得到了ErrorMessage:“由于客户端数据的原因,异常已经被抛出”。
InnerException:-空,
为了捕获实际的异常,我尝试了下面的代码:
public RegisterResponse1 RegisterAccount()
{
try
{
var upss = CreateAccessRequest();
//Process Request
var responce = regService.ProcessRegister(CreateRegisterWebServiceRequest(shipment));
return responce;
}
catch (SoapException ex)
{
//I never go here
return null;
}
catch (FaultException ex)
{
//always go there
return null;
}
catch (Exception ex)
{
return null;
}
}在上面的异常处理中,我总是从FaultException修改到FaultException (例如)上面的errorMessaeg
当我尝试从SoapUI(readyAPI)工具手动请求此API时,我得到了以下错误详细信息,这是来自API端的实际错误。我希望在我的c#库中出现该错误。请参阅下面的实际错误详细信息。

"Invalid access License Number“是我想要获取的实际消息
请帮助我捕获c#中的实际错误详细信息"Invalid access License Number“,而不是error-exception已因客户数据而引发
提前谢谢你
发布于 2018-11-01 01:36:31
是的,我在自己的项目中与这个问题斗争了很长一段时间,最终找到了解决方案。WSDL生成器存在错误生成错误对象的问题,这就是您无法访问它的原因。
要诊断幕后发生了什么,您可以使用Fiddler之类的工具,并监控流量。UPS正在返回故障详细信息,其中包含一条消息,该消息将告诉您发生了什么问题,但由于生成的对象不正确,因此您看不到它。
要在任何UPS中解决此问题,请进入生成的Reference.cs,并在名称空间声明之后添加以下类:
// The WSDL generator doesn't properly generate the Errors Class. This was taken from the UPS Samples. The FaultContractAttribute was also updated to use this class instead.
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.2612.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1")]
public partial class Errors : object, System.ComponentModel.INotifyPropertyChanged
{
private ErrorDetailType[] errorDetailField;
private string testElementField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ErrorDetail", Order = 0)]
public ErrorDetailType[] ErrorDetail
{
get
{
return this.errorDetailField;
}
set
{
this.errorDetailField = value;
this.RaisePropertyChanged("ErrorDetail");
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Order = 1)]
public string TestElement
{
get
{
return this.testElementField;
}
set
{
this.testElementField = value;
this.RaisePropertyChanged("TestElement");
}
}
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));
}
}
}然后在文件中搜索FaultContractAttribute。将其更改为:
[System.ServiceModel.FaultContractAttribute(typeof(Errors), Action="http://onlinetools.ups.com/webservices/ShipBinding/v1.1", Name="Errors", Namespace="http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1")]您可能需要调整typeof()语句中的命名空间。
然后,像这样的块将捕获/返回错误消息:
catch (FaultException<Errors> ex)
{
return new MyResult
{
ErrorMessage =
$"UPS returned the following errors: {string.Join(", ", ex.Detail.ErrorDetail.Select(ed => ed.PrimaryErrorCode.Description))}"
};
}这将为您提供UPS API调用的全部错误,您可以从中阅读API文档以了解错误所在。
发布于 2020-02-24 23:20:55
在我的例子中,真正的错误描述是:
客户集成环境中不支持
Hard9264030The状态。
我意识到使用我目前的沙箱凭证,只有美国的NY和CA州是有效的。因此,我建议您以这种方式更改代码,以检查为您显示的错误。
public RegisterResponse1 RegisterAccount()
{
try
{
var upss = CreateAccessRequest();
//Process Request
var responce = regService.ProcessRegister(CreateRegisterWebServiceRequest(shipment));
return responce;
}
catch (SoapException ex)
{
//=====================================================================
//put this code here and you will see the correct description of the error
var error = ex.Detail.InnerText;
Console.WriteLine(error);
return null;
}
catch (FaultException ex)
{
//always go there
return null;
}
catch (Exception ex)
{
return null;
}
}https://stackoverflow.com/questions/47766873
复制相似问题