Catching SOAP Errors and Handling Exceptions

I am using web services. Some methods throw an exception when calling i because, for example, parameters are invalid values. I want to handle exceptions, but it does not contain any information about the data, just a Bad Request message. This is my http response: enter image description here

        try
        {
            var data = client.SomeMethod(4);
        }
        catch (Exception exception)
        {
            // exception.Message = Bad Request
            // exception don't contains any more data information
        }   

      

How can I capture other information

+3


source to share


2 answers


You can catch the exception with FaultException

if the http status code is 2xx or 5xx and not 4xx. You can catch the httpxx status code with System.ServiceModel.ProtocolException

and then get the stream from InnerException

and parse it, or get a Fault exception from that stream. See http://blogs.msdn.com/b/nathana/archive/2011/03/31/deciphering-a-soap-fault-with-a-400-status-code.aspx for details .



+3


source


I'm assuming this is a WCF web service? You are catching a lot of exceptions. Try it FaultException<TDetail>

.



Typical deployed services use the FaultContractAttribute to formally indicate any SOAP errors that a client might expect to receive during a normal operation. Error information in the FaultContractAttribute appears as a FaultException (where typeparameter is the serializable error object specified in the FaultContractAttribute operation) when it arrives at the client application. The FaultContractAttribute can be used to indicate SOAP faults for both two-way service methods and asynchronous method pairs.

Because FaultException is both FaultException and therefore CommunicationException, in order to catch specific SOAP errors, make sure you catch FaultException types before FaultException and CommunicationException types, or handle the specified exceptions in one of these exception handlers.

+2


source







All Articles