3

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

user20358
  • 14,182
  • 36
  • 114
  • 186

3 Answers3

7

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.

user20358
  • 14,182
  • 36
  • 114
  • 186
  • This is the actual solution. Dave's reply helped me get to it, so I credited his reply. – user20358 Jan 31 '11 at 06:52
  • Hello, I was banging my head with the wall until I found your question and your solution worked like magic! Can you please explain what this line is doing? `System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;` – Huma Ali Feb 10 '16 at 04:38
3

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

Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
2

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.

DaveB
  • 9,470
  • 4
  • 39
  • 66
  • so Silverlight was designed without service error handling in mind? :O – user20358 Jan 30 '11 at 11:13
  • Looks like the status code must be changed from 500 to what Silverlight can read ..that too on the service side. So what if its a service that I have no control over? I think that is a lame workaround. Gonna read this one more time to see if I missed something! – user20358 Jan 30 '11 at 11:15
  • I have done exactly what is mentioned in that article for handling Undeclared Faults; still getting the same error:'System.Net.WebException'. – user20358 Jan 30 '11 at 11:23