Question

The problem is similar to this question: Does WCF FaultException<T> support interop with a Java web service Fault. However, in my case, the custom fault is indeed returned from the service.

Tricky to explain this but here it goes:

From a new WCF client, I’m calling an existing Java web service (that has worked well for years with other kinds of clients). The service XSD defines one custom fault ServiceFault:

<xs:complexType name="ServiceFault">
        <xs:annotation>
            <xs:documentation>On service error</xs:documentation>
        </xs:annotation>
        <xs:all>
            <xs:element name="ErrorCode" type="xs:string"/>
            <xs:element name="ErrorDescription" type="xs:string"/>
        </xs:all>
    </xs:complexType>

This ServiceFault is used for all more specific custom faults (eg. MyMethodFault) so they all get the ErrorCode and ErrorDescription properties, like so:

<xs:element name="MyMethodFault" type="ServiceFault">
    <xs:annotation>
        <xs:documentation>On error in MyMethod</xs:documentation>
    </xs:annotation>
</xs:element>

Next, the WSDL-messages for the faults are defined, like this:

<wsdl:message name="MyMethodFault">
<wsdl:part element="tns:MyMethodFault " name="MyMethodFault">
</wsdl:part>

And finally, the operations are defined with those faults, like this:

<wsdl:fault message="tns:MyMethodFault" name="MyMethodFault">

Looks good to me, so far. And at runtime, the service neatly returns the faults in the detail SOAP-tag, like this:

<ns2:MyMethodFault xmlns:ns2="urn:salessystem:entity:MyService:v1.0">
<ns2:ErrorCode>1000</ns2:ErrorCode>
<ns2:ErrorDescription>TheErrorDescr</ns2:ErrorDescription>
</ns2:MyMethodFault>

However, SvcUtil didn’t generate the more specific custom faults, only the ServiceFault. This is the only mentioning of MyMethodFault in the service reference:

[OperationContractAttribute(Action = "", ReplyAction = "*")]
[FaultContractAttribute(typeof(sale...ServiceFault), Action="", Name="MyMethodFault")]        
[ServiceKnownTypeAttribute(typeof(ServiceFault))]
MyMethodResponse MyMethod(MyMethodRequest1 request);

So, here is the problem:

Since MyMethodFault doesn’t exist I can’t catch FaultException<MyMethodFault>. And furthermore, WCF won’t map a specific fault like MyMethodFault to a ServiceFault at runtime so exceptions of type FaultException<ServiceFault> are never caught. Hence, I never get the error description.

Am I doing something wrong or do I need to tweak the WSDL or the SvcUtil-usage?

Hope anyone can understand the question at all :P

Thanks, Björn

Was it helpful?

Solution

Sometimes a little vacation is all you need. Problem solved:

SvcUtil generated not one, but two C# types for the generic custom fault ServiceFault. The other one was located deeper in the namespace hierarchy. And trying to catch that other fault works just fine. All the more specific custom faults (that derive from ServiceFault in the XSD) are caught here and I can see the error code and description of the fault. That's all I need right now.

However, I still have no C# types generated for the more specific custom faults such as MyMethodFault described in the XSDs. But maybe I'm not supposed to!

What I failed to describe in the code examples above is that the specific fault MyMethodFault’s XML in the SOAP response is in inside the Detail-tag of the returned fault:

<SOAP-ENV:Fault>
  <faultcode>SOAP-ENV:Server</faultcode>
  <faultstring xml:lang="en">ServiceException</faultstring>
  <detail>
    <ns2:MyMethodFault xmlns:ns2="urn:salessystem:entity:MyService:v1.0">
      <ns2:ErrorCode>1000</ns2:ErrorCode>
      <ns2:ErrorDescription>My+Error+Description</ns2:ErrorDescription>
    </ns2:UcsAgreemenFault>
  </detail>
</SOAP-ENV:Fault>

This fault gets mapped to the "base" ServiceFault in my try/catch. In the SOAP above, you can clearly see the specific fault’s type or name. I don’t know how to get hold of that type/name when the exception is caught, though. All I see is the ErrorCode and ErrorDescription. However, all is well as long as I can catch the ServiceFault and see the code and description.

Thanks for any thoughts put in to this or any further comments.

/Björn

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top