Dan Rigsby - Coding Up Style

Developer.Speaker.Blogger

Async Operations in Wcf: Handling Exceptions

Posted by Dan Rigsby on August 28th, 2008

Async Operations in Wcf Series:
Part 1: Event Based Model
Part 2: IAsyncResult Model (Client-Side)
Part 3: IAsyncResult Model (Server-Side)
Part 4: Canceling Operations
Part 5: Handling Exceptions

This is the fifth in a five part series on asynchronous operations in Wcf.  If you are new to this series, you might want to read the first section of part one to get an introduction to this post: http://www.danrigsby.com/blog/index.php/2008/03/18/async-operations-in-wcf-event-based-model/

What happens when there is an exception

We have covered the ins and outs of working with asynchronous operations.  But what happens in the unexpected happens?  What happens if an Exception or FaultException occurs on the server? There are basically two situations:

  1. The exception is thrown before the thread is queued or before the thread context switch has happened.  This would still be on the users’s initiated thread.
  2. The exception is thrown during the actual asynchronous operation on a different thread. This would be on some internal thread in the WCF service.

In situation 1, this acts like any other service call.  Since the exception occurred on the same thread the client initiated, it is simply propagated back to the client like any other exception.

In situation 2, the exception occurs on a separate thread, so the client isn’t directly notified. Instead the server caches the exception and signals the client that the operation is complete.  The client in turn will call its end operation completion method which will throw the exception back to the client.  Keep in mind though, that if the exception is fatal to the service and beyond recovery, it can crash the service process itself.

Exception example

An example of this can be shown with a simple service that calculates a square root.  In the call on the server, we have it wait 20 seconds before returning the result.  However, if we throw an exception before the wait period, the client will get signaled immediately. 

public double GetSquareRoot(double value)
{
    // Throwing exception here causes the server to signal to 
    // the client that the operation is complete and the exception
    // is rethrown on the client
    throw new FaultException("Fault before queuing thread.");

    Thread.Sleep(20000); // Wait 20 seconds

    return Math.Sqrt(value);
}
 

AsyncException

You can download this full example here: http://www.danrigsby.com/Files/Rigsby.WcfAsyncOperationExceptions.zip

DotNetKicks Image

6 Responses to “Async Operations in Wcf: Handling Exceptions”

  1. Dan Rigsby » Async Operations in Wcf: Canceling Operations Says:

    [...] Posted by Dan Rigsby on March 30th, 2008 Async Operations in Wcf Series: Part 1: Event Based Model Part 2: IAsyncResult Model (Client-Side) Part 3: IAsyncResult Model (Server-Side) Part 4: Canceling Operations Part 5: Handling Exceptions [...]

  2. Dan Rigsby » Async Operations in Wcf: IAsyncResult Model (Server-Side) Says:

    [...] Posted by Dan Rigsby on March 26th, 2008 Async Operations in Wcf Series: Part 1: Event Based Model Part 2: IAsyncResult Model (Client-Side) Part 3: IAsyncResult Model (Server-Side) Part 4: Canceling Operations Part 5: Handling Exceptions [...]

  3. Dan Rigsby » Async Operations in Wcf: IAsyncResult Model (Client-Side) Says:

    [...] Posted by Dan Rigsby on March 20th, 2008 Async Operations in Wcf Series: Part 1: Event Based Model Part 2: IAsyncResult Model (Client-Side) Part 3: IAsyncResult Model (Server-Side) Part 4: Canceling Operations Part 5: Handling Exceptions [...]

  4. Dan Rigsby » Async Operations in Wcf: Event Based Model Says:

    [...] Posted by Dan Rigsby on March 18th, 2008 Async Operations in Wcf Series: Part 1: Event Based Model Part 2: IAsyncResult Model (Client-Side) Part 3: IAsyncResult Model (Server-Side) Part 4: Canceling Operations Part 5: Handling Exceptions [...]

  5. Arjan`s World » LINKBLOG for August 28, 2008 Says:

    [...] Async Operations in Wcf: Handling Exceptions - Dan Rigsby [...]

  6. Dew Drop - August 29, 2008 | Alvin Ashcraft's Morning Dew Says:

    [...] Async Operations in WCF: Handling Exceptions (Dan Rigsby) [...]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>