This project is read-only.

Error Handling Block

Provides simple handling of transient errors that can occur with any network communication. Retries, intervals, mapping and detection can all be fully customized. Built-in handlers are provided for WCF, WebClient, HttpRequest. Supports .NET 4 and later, Silverlight 4 and 5, Windows Store Apps, and Windows Phone 7.5 and later.

This project was inspired by Microsoft's Exception Handling Application Block which is far more well-rounded in every which way except it doesn't support all the platforms this does, which may or may not be important to you. If not, then go check it out.

Why and How
Errors will occasionally happen, particularly when communicating over networks. No matter how reliable it is, a glitch will eventually occur and you want your software to handle it. So you wrap all your communication calls in a try/catch just for that rare case. And you pop a message box that ever so politely informs the user victim that despite your best efforts, something completely out of your hands has gone wrong and they'll need to restart the app, or at the very least the last operation.

So much better than just letting the exception fly and crashing the app, right? Not really. It's more polite but they'll still hate you when it happens.

How much better would it be if the user saw... nothing. Perhaps they'd sense a slight delay but then... it works. Now they like you.

The Error Handling Block is a simple yet highly customizable way to handle transient errors by automatically retrying the operation some number of times with some amount of delay between each attempt. Here's a quick before and after:

BEFORE:
try
{
   await SomeWebOperation();
}
catch (Exception e)
{
   MessageBox.Show("We are so very sorry...");
}

AFTER:
try
{
   await errorHandler.Run(() => SomeWebOperation());
}
catch (Exception e)
{
   MessageBox.Show("We are so very sorry...");
}

A tiny little change and suddenly you've got automatic retries of transient errors, immediate failure of permanent errors. Yes, it can still fail and you'll still need to beg forgiveness and they'll still hate you when something major goes wrong, but those occasional glitches due to network hiccups will no longer appear.

Of course, there's some setup code you have to do ahead of time to create that "errorHandler" that was used in the code above. Here's how that looks:

Creating WCF error handler:
   var errorHandler = new WcfErrorHandler();

Ok, so maybe that wasn't fair because it's the included error handler that deals with transient and permanent errors typically encountered by clients using WCF (Windows Communication Foundation).

And if you use WebClient or HttpRequest then there's an included handler for that, too.

Surely there's something you are doing that requires a custom handler, in which case it's really not very hard to customize one of the existing retry plans or to write your own. Or to create a custom error detector/mapper. And the source is available, too, so you can look at the ones that are included for guidance.


Last edited Feb 26, 2013 at 5:27 AM by MikeRiedel, version 4

Comments

No comments yet.