All the service calls in my application are implemented as tasks.When ever a task is faulted ,I need to present the user with a dialog box to retry the last operation failed.If the user chooses retry the program should retry the task ,else the execution of the program should continue after logging the exception.Any one has got a high level idea on how to implement this functionality ?
All the service calls in my application are implemented as tasks.When ever a task
Share
UPDATE 5/2017
C# 6 exception filters make the
catchclause a lot simpler :and a recursive version:
ORIGINAL
There are many ways to code a Retry function: you can use recursion or task iteration. There was a discussion in the Greek .NET User group a while back on the different ways to do exactly this.
If you are using F# you can also use Async constructs. Unfortunately, you can’t use the async/await constructs at least in the Async CTP, because the code generated by the compiler doesn’t like multiple awaits or possible rethrows in catch blocks.
The recursive version is perhaps the simplest way to build a Retry in C#. The following version doesn’t use Unwrap and adds an optional delay before retries :
The StartNewDelayed function comes from the ParallelExtensionsExtras samples and uses a timer to trigger a TaskCompletionSource when the timeout occurs.
The F# version is a lot simpler:
Unfortunatley, it isn’t possible to write something similar in C# using async/await from the Async CTP because the compiler doesn’t like await statements inside a catch block. The following attempt also fails silenty, because the runtime doesn’t like encountering an await after an exception:
As for asking the user, you can modify Retry to call a function that asks the user and returns a task through a TaskCompletionSource to trigger the next step when the user answers, eg:
With all the continuations, you can see why an async version of Retry is so desirable.
UPDATE:
In Visual Studio 2012 Beta the following two versions work:
A version with a while loop:
and a recursive version: