Suppose you have a method asyncas shown below:
public async Task<TResult> SomeMethod1()
{
throw new Exception();
}
public async Task<TResult> SomeMethod2()
{
await Task.Delay(50);
throw new Exception();
}
Now, if you are awaitfor these 2 methods, the behavior will be almost the same. But if you get the job, the behavior is different.
If I want to cache the result of such a calculation, but only when the task completes. I have to take care of situation 2:
The first situation:
public Task<TResult> CachingThis1(Func<Task<TResult>> doSomthing1)
{
try
{
var futur = doSomthing1()
futur.ContinueWith(
t =>
{
},
TaskContinuationOptions.NotOnFaulted);
}
catch ()
{
throw;
}
}
Second situation
public Task<TResult> CachingThis2(Func<Task<TResult>> doSomthing)
{
var futur = SomeMethod2();
futur.ContinueWith(
t =>
{
},
TaskContinuationOptions.NotOnFaulted);
futur.ContinueWith(
t =>
{
},
TaskContinuationOptions.OnlyOnFaulted);
}
Now I am passing into my cache system a method that will perform the calculation in the cache.
cachingSystem.CachingThis1(SomeMethod1);
cachingSystem.CachingThis2(SomeMethod2);
Clearly, I need to duplicate the code in " ConinueWithon faulted" and the catch block. Do you know if there is a way to make the exception the same as before or after waiting?