Trevor Daniel
Trevor Daniel

Reputation: 3974

Accessing XML Webservice Exception Object

I am calling an XML webservice. I am using the following function:

[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);

When there is nothing wrong with the request i have sent the result happily returns a "ShipmentResponse1"

But if there is something wrong with the request I cannot work out how to get to the error details as shown here in Fiddler:

enter image description here

I have wrapped my code in a basic try/catch (Exception ex) but the ex only contains the "faultstring".

I'd like to be able to get to the "PrimaryErrorCode Code and Description" but cannot work out how to do it.

Any help would be greatly appreciated

Upvotes: 2

Views: 124

Answers (1)

pfx
pfx

Reputation: 23254

The ProcessShipmentAsync method is decorated with a FaultContractAttribute, which specifies the type of the error details, here : 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"
    )]

To access these in case of an error, you have to catch a FaultException<T>, where T is that UPS.ShipServiceReference.ErrorDetailType[] type.
The detailed info is available on the Details property of that exception.

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

Upvotes: 2

Related Questions