Abh*_*eet 2 c# api wsdl web-services
在 C# 中访问 SoapAPI 我在我的 c# 类中添加了 WSDL 文件作为 SeriviceRefrence 一些我无法捕获该 API 抛出的实际异常的原因
我收到 ErrorMessage:-“由于客户端数据而引发异常”。
内部异常:-空,
为了捕获实际异常,我尝试了以下代码:-
 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# 库中看到以下实际错误详细信息

“无效的访问许可证号”是我想要获取的实际消息
请帮助我捕获 c# 中的实际错误详细信息“无效的访问许可证号”,而不是由于客户端数据而引发错误异常
先感谢您
是的,我在自己的项目中与这个问题斗争了很长时间,终于找到了解决方案。WSDL 生成器存在错误生成故障对象的问题,这就是您无法访问它的原因。
要诊断幕后情况,您可以使用 Fiddler 等工具并监控流量。UPS 正在返回一个故障详细信息,其中包含一条消息,告诉您出了什么问题,但由于生成的对象错误,您看不到它。
要在任何 UPS API 中解决此问题,请进入生成的 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));
        }
    }
}
然后在文件中搜索 for 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 文档以了解问题所在。
| 归档时间: | 
 | 
| 查看次数: | 2972 次 | 
| 最近记录: |