10

We've got REST and SOAP endpoints for our service so we use WebFaultException to pass friendly messages. This works great for the REST calls not so much for the SOAP calls. Below is the trace which clearly shows the friendly message in the "detail" element. But the FaultException that is raised on the client has the http status code description in the message - not the real message thrown from the service. Is there any way to surface the intended message on the client?

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Header></s:Header>
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/2009/WebFault" xmlns="">a:BadRequest</faultcode>
         <faultstring xml:lang="en-US" xmlns="">Bad Request</faultstring>
         <detail xmlns="">
            <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">Country code must be 3 characters.</string>
         </detail>
      </s:Fault>
   </s:Body>
</s:Envelope>

Also, this is in .net 4.0 and we are using Castle's WCF facility (DefaultServiceModel and RestServiceModel).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Michael
  • 103
  • 1
  • 1
  • 4

1 Answers1

13

WCF will - by default and by design - not report back detailed error info for security reasons. It basically will only tell you "something went wrong on the server - bad luck".

You can - for development and testing purposes - enable more detailed error info, but you should turn that off for production.

To enable that, use a service behavior on your server:

<system.serviceModel>
   <behaviors>
      <serviceBehaviors>
         <behavior name="DetailedDebugging">
             <serviceDebug includeExceptionDetailInFaults="True" />
         </behavior>
      </serviceBehaviors>
   </behaviors>

   <services>
      <service name="YourService"
               behaviorConfiguration="DetailedDebugging" >
          ....
      </service>
   </services>
</system.serviceModel>

Now your service should report back the detailed SOAP fault including all details, all the way back to your client app.

Update: if I remember correctly, when handling a standard (non-typed) FaultException, you have easy access to stuff like the FaultCode and FaultReason etc., but the message details are a bit cumbersome to get it - try something like this:

try
{
   // your service call here
}
catch(FaultException fe)
{
   FaultCode fc = fe.Code;
   FaultReason fr = fe.Reason;

   MessageFault mf = fe.CreateMessageFault();
   if(mf.HasDetail)
   {
      string detailedMessage = mf.GetDetail<string>();
   }
}

Does that give you access to the detailed description of your SOAP fault??

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    That's not what it is and I've tried that for debug purposes. The entire point of WebFaultException and FaultException is to return custom messages. That's why the WCF guys put it in there. – Michael Feb 25 '11 at 19:48
  • @Michael: custom messages doesn't imply custom exceptions. – John Saunders Feb 25 '11 at 20:04
  • Any idea of a nice way of getting the text when it gives you `Message here`? `string` won;t deserialise it as it doesn't recognise it. – Deanna May 01 '14 at 16:24