How do I access the <detail> section in a SOAP Fault when there is no FaultContract defined?

792 views Asked by At

I am using WCF to connect to a business partner's web service. The web service does not have a defined fault contract - there are no <wsdl:fault> elements in the WSDL.

When a fault occurs I get back a response like so (namespaces pruned for readability):

<s:Envelope>
   <s:Body>
      <Fault>
         <faultcode>xxx</faultcode>
         <faultstring>Business data error</faultstring>
         <detail>
            <Error>
               <ErrorCode>xxx</ErrorCode>
               <ErrorDescription>xxx</ErrorDescription>
            </Error>
         </detail>
      </Fault>
   </s:Body>
</s:Envelope>

In my code I can catch the exception like so:

try
{
    proxy.DoWork();
}
catch(FaultException fex)
{
    ...
}

But because there is no defined FaultContract I can't use the generic-based FaultException (like catch(FaultException<myFaultType>)).

Long story short, I need to be able to examine the <ErrorCode> and <ErrorDescription> elements in the <Error> element returned in the SOAP fault in the catch above.

Thanks

1

There are 1 answers

0
Jose M. On

You can use a example of the response to generate a schema with command xsd:

// xsd foo.xml
<s:Envelope xmlns:s="foo.com">
 <s:Body>
  <Fault>
     <faultcode>xxx</faultcode>
     <faultstring>Business data error</faultstring>
     <detail>
        <Error>
           <ErrorCode>xxx</ErrorCode>
           <ErrorDescription>xxx</ErrorDescription>
        </Error>
     </detail>
  </Fault>
</s:Body>

Then, you can generate the serealization classes with the same command:

// xsd foo.xsd /c
...
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="foo.com")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="foo.com", IsNullable=false)]
public partial class Envelope {
...

Maybe you have to edit some attribute of the class foo.c, but I think this should be work.