1

I'm calling a WCF service from a Silverlight client, therefore making all the calls async.

When the WCF service returns an ExceptionFault, the error object gets populated in the arguments to the Completed event handler...

proxy.UpdateCompleted += (o, e) =>
 {
     if(e.Error != null)
            .... do something
 }

what is the best approach for converting e.Error into a FaultException?

if (e.Error is FaultException<InvalidOperationFault>)
{
  var fault = e.Error as FaultException<InvalidOperationFault>;    
  ... do something
}

or

try
{
   throw e.Error
}
catch(FaultException<InvalidOperationFault> fault)
{
   ... do something
}

or can you suggest anything better?

Andronicus
  • 1,799
  • 1
  • 14
  • 28

2 Answers2

2

I prefer the second option, to throw e.Error if it is not null. That way the resulting code is structured the same as in the synchronous method invocation case:

try
{
  if ( e.Error != null ) 
    throw e.Error;
  // ...
}
catch ( FaultException<ValidationFault> fault ) { /* ... */ }
catch ( FaultException<MyServiceFault> fault ) { /* ... */ }
// ...
finally { /* ... * }
  • I have a concern that this technique may change the stack trace to make it appear that the exception came from the point of the `throw`. – John Saunders Jan 08 '15 at 19:11
0

This just boils down to the lack of Switch on type support in C#

Is there a better alternative than this to 'switch on type'?

Community
  • 1
  • 1
Andronicus
  • 1,799
  • 1
  • 14
  • 28