Web Services 访问XML Web服务异常对象

kiayqfof  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(195)

我正在调用一个XML Web服务。我正在使用以下函数:

[System.ServiceModel.OperationContractAttribute(Action="http://onlinetools.ups.com/webservices/ShipBinding/v1.0", ReplyAction="*")]
    [System.ServiceModel.FaultContractAttribute(typeof(UPS.ShipServiceReference.ErrorDetailType[]), Action="http://onlinetools.ups.com/webservices/ShipBinding/v1.0", Name="Errors", Namespace="http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(ShipmentServiceOptionsType))]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(CompanyInfoType))]
    System.Threading.Tasks.Task<UPS.ShipServiceReference.ShipmentResponse1> ProcessShipmentAsync(UPS.ShipServiceReference.ShipmentRequest1 request);

当我发送的请求没有任何错误时,结果将愉快地返回“ShipmentResponse 1”
但如果请求有问题,我无法找到如何获取错误详细信息,如Fiddler中所示:

我已经将我的代码 Package 在一个基本的try/catch(异常ex)中,但是该ex只包含“faultstring”。
我希望能够访问“PrimaryErrorCode代码和说明”,但无法找到具体方法。
如有任何帮助,我将不胜感激

yquaqz18

yquaqz181#

ProcessShipmentAsync方法使用FaultContractAttribute修饰,它指定错误详细信息的类型,如下所示:UPS.ShipServiceReference.ErrorDetailType[] .

[System.ServiceModel.FaultContractAttribute(  
    typeof(UPS.ShipServiceReference.ErrorDetailType[]),  
    Action="http://onlinetools.ups.com/webservices/ShipBinding/v1.0",  
    Name="Errors", Namespace="http://www.ups.com/XMLSchema/XOLTWS/Error/v1.1"
    )]

要在出错时访问这些对象,必须捕获FaultException<T>,其中T是UPS.ShipServiceReference.ErrorDetailType[]类型。
有关详细信息,请参阅该异常的Details属性。

catch (FaultException<UPS.ShipServiceReference.ErrorDetailType[]> ex)
{
    // Access ex.Details.
}

相关问题