Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF fault exception doesn't surface detail on client for soap service

Tags:

wcf

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).

like image 758
Michael Avatar asked Feb 25 '11 16:02

Michael


1 Answers

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??

like image 135
marc_s Avatar answered Sep 30 '22 17:09

marc_s