Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle incorrect SOAP fault in WCF?

I have to consume to a third-party web service using SOAP. It was easy to get it to work with WCF, but now I have a problem with SOAP faults. The service sends me an incorrect SOAP fault:

<?xml version="1.0" encoding="utf-8" ?>
<SOAP-ENV:Envelope 
    SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" 
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Body>
        <SOAP-ENV:Fault>
            <SOAP-ENV:faultcode>14</SOAP-ENV:faultcode>
            <SOAP-ENV:faultstring>Unknown Function</SOAP-ENV:faultstring>
        </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

The error is that the <faultcode> must not have a namespace:

System.ServiceModel.CommunicationException : Server returned an invalid 
SOAP Fault. Please see InnerException for more details.
    ----> System.Xml.XmlException : Start element 'faultcode' from namespace 
    '' expected. Found element 'SOAP-ENV:faultcode' from namespace 
    'http://schemas.xmlsoap.org/soap/envelope/'.

I can't change the original web service - but is there anything in WCF I can use to still handle these fault messages in some way without getting CommunicationException all the time?

like image 636
chris166 Avatar asked Feb 18 '10 09:02

chris166


People also ask

How are errors handled in SOAP?

If an error occurs during processing, the response to a SOAP message is a SOAP fault element in the body of the message, and the fault is returned to the sender of the SOAP message.

How do you handle a fault exception in C#?

In a service, use the FaultException class to create an untyped fault to return to the client for debugging purposes. In a client, catch FaultException objects to handle unknown or generic faults, such as those returned by a service with the IncludeExceptionDetailInFaults property set to true .

What is a SOAP exception error?

A SOAP fault is an error in a SOAP (Simple Object Access Protocol) communication resulting from incorrect message format, header-processing problems, or incompatibility between applications.


1 Answers

Yes, but it's not exactly elegant. See the message inspector code at the bottom of this forum post:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/435850aa-bf74-4158-a29a-256135207948

Basically, you can take the incoming message and alter it so that it can be handled by WCF.

like image 96
WayneC Avatar answered Sep 27 '22 23:09

WayneC