SOAP Error Webservice - Design Practices

I need to design a SOAP api (my first!). What are the best practices regarding errors returned to the caller.

Assuming api as follows

[WebMethod] public List<someClass> GetList(String param1) { } 

Should I

  • Throw an exception. Let the SOAP infrastructure generate a SOAP error - and the caller should try / catch. This is not very clear to the caller.
    1. Have a return parameter as some kind of XMLDOcument, with the first element being the return value and then List.
    2. Looking at the returned SOAP package, I see that the generated response is as follows

  <GetListResponse> <GetListResult> ... ... </GetListResult> </GetListResponse> 

Is it possible to somehow change the returned package so that the GetListResult element is changed to GetListError in case of an error

  • Any other way?

Thanks!

+4
source share
4 answers

Probably the most appropriate SOA pattern for later use will be a fault contract, which is essentially a data contract that is wrapped in a SOAPException.

I am posting examples in .NET, as it looks like this is what you are using (and this is what I know :))

In WCF, you can define a DataContract and then decorate your OperationContract interface with the "FaultContract" attribute, which indicates it as a return value:

 public partial interface MyServiceContract { [System.ServiceModel.FaultContract(typeof(MyService.FaultContracts.ErrorMessageFaultContract))] [System.ServiceModel.OperationContract(...)] ResponseMessage SOAMethod(RequestMessage request) {...} } 

For ASMX web services (as you seem to use from a piece of code), you cannot use this attribute or setting. Therefore, to implement the template you need:

  • Define a serializable class for storing exception information (i.e. ErrorData)
  • When an exception is thrown in your service, catch it and add information to the ErrorData class in the error handling code li>
  • Add the serialized ErrorData class to the SoapException class:

     SoapException mySoapException = new SoapException(message, SoapException.ServerFaultCode, "", serialzedErrorDataClass); 
  • Throw a SoapException in your code

  • On your client side, you need to deserialize the message in order to interpret it.

You seem to have a lot of work to do, but in this way you have complete control over what data is being returned. By the way, this is a template used by ServiceFactory from Microsoft templates and methods for ASMX web services.

+2
source

You can cause the correct error to be returned from older ASMX services, but this is not easy. First of all, you will have to manually encode the WSDL, because the ASMX infrastructure will never create Fault elements in the WSDL. Then you must serialize the desired damage data into an XmlElement, which will then be presented as the Detail property for the SoapException that you will throw.

It is much easier with WCF. You can declare several FaultContracts for each operation, and WCF will generate the correct WSDL to refer to them. Then you just throw a FaultException, where type T is type FaultContract. Pass the instance of T to the constructor, and you're all set up.

+1
source

I can't give you directions for .net (which seems to be what you are asking for), but SOAP provides a mechanism for expressing strongly typed exceptions. The SOAP error element may have an optional FaultDetail sub-element, and it may contain arbitrary XML documents such as GetListError. These types of documents must be defined in WSDL as wsdl: error inside wsdl operation:

The trick convinces the web services stack to turn the exception (which is the "right" way of writing your business logic) into proper granularity of the failures. And I can not help you with this.

0
source

All Articles