Search code examples
silverlightwcffaultexception

silverlight client doesnt understand faultexception


I have a WCF service that throws an exception which I am trying to catch unsucessfully in my silverlight client code. I am using Undeclared Faults for Debugging purposes and this is my service method :

[OperationContract]
public ServiceResponse MyWCFServiceMethod()
{
  ServiceResponse resp = new ServiceResponse ();
  //code for setting resp...

  //purposely throw error here.
  throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));
  return resp;
}

Now in my silverlight client view model, in the service's callback method, I try to handle it like this:

private void MyServiceCallback(MyWCFServiceMethodCompletedEventArgs e)
{
   if (e.Error == null)
   {
       //proceed normally
   }
   else if (e.Error is FaultException)
   {
      FaultException<ExceptionDetail> fault = e.Error as FaultException<ExceptionDetail>;
      MessageBox.Show(fault.Detail.Message);
      MessageBox.Show(fault.Reason.ToString());
   }
} 

at this line else if (e.Error is FaultException) I still get System.Net.WebException {The remote server returned an error: NotFound.}

These are the config entries

<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />

This is the service class declaration

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MySilverlightWCFService
{
 ....

This service is in another project within the same silverlight solution. Why is my silverlight client not able to get the fault exception I am throwing?

Thanks for your time...


Solution

  • The server is probably throwing a HTTP 500 response code that Silverlight is ignoring. You must change the service to return a HTTP code that Silverlight will accept.

    From Data Performance and Fault Strategies in Silverlight 3: (This article will show you how to return WCF faults to Silverlight.)

    Infamous NotFound Error: When the exception is raised, an HTTP status code of 500 is returned to Silverlight. The browser networking stack prevents Silverlight from reading responses with a status code of 500, so any SOAP fault information contained within is unavailable to the Silverlight client application. Even if the message could be retrieved, Silverlight 2 is not capable of converting the fault back into a managed exception. Both of these issues have been addressed in Silverlight 3.