silverlight client doesnt understand faultexception

3.6k views Asked by At

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

3

There are 3 answers

3
DaveB On BEST ANSWER

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.

2
user20358 On

ok so finally what seems to be the way to make this work is to get one line of code added to the service just before you throw the fault exception!

System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;

Then throw the actual exception:

throw new FaultException(new FaultReason(new FaultReasonText("My fault Reason!")),new FaultCode("my fault code here"));

Then in silverlight modify the service call back error handling section from what I put in my question above to:

   //else if (e.Error is FaultException)
   else
   {
      //FaultException<ExceptionDetail> fault = e.Error as FaultException<ExceptionDetail>;
      //MessageBox.Show(fault.Detail.Message);
      FaultException fault = e.Error as FaultException;
      MessageBox.Show(fault.Reason.ToString());
   }

That worked for me. Ugly way!

I will try with Declared faults when I get the time.

0
Ronnie Overby On

Check out the SilverlightFaultBehavior. It will handle changing the status code for you.