2

I have an implementation like this:

Task<IEnumerable<Item1>> GetItems1() 
{
    return RunRequest(async () => ParseItemsFromResponse(await(httpClient.Get(..))));
}

Task<IEnumerable<Item2>> GetItems2() 
{
    return RunRequest(async () => ParseItemsFromResponse(await httpClient.Get(..)));
}


TResult RunRequest<TResult>(Func<TResult> req)
{
    try
    {
        return req();
    }
    catch (Exception ex)
    {
        // Parse exception here and throw custom exceptions
    }
}

The issue is the void anonymous method async () => ParseItemsFromResponse(..).

Since it returns void and not a Task, if there's an exception thrown within the anonymous method, it's actually not going to be caught by the try and catch within the RunRequest.

Any suggestions how to refactor this?


  • Just don't use async void. They are only meant for event handlers or similar methods that can't receive or handle a Task result. You can't an exception thrown by async void except by the application's Unhandled Exception handler - Panagiotis Kanavos
  • Generally it's advised never to use void with async functions. - Bauss
  • Make RunRequest take a Task instead of Func and pass in the httpClient.Get() directly? - Mats391
  • The compiler wouldn't use that RunRequest overload unless ParseItemsFromResponse returns something. Do you have another one for Action ? Just don't do it like this. var result=await httpClient.Get();ParseItems(result); is cleaner - Panagiotis Kanavos
  • @Mats391 that's a good idea, why I didn't think about it, Thanks! - Don Box

1 답변


3

RunRequest should take a Func<Task<TResult>>, as such:

async Task<TResult> RunRequestAsync<TResult>(Func<Task<TResult>> req)
{
  try
  {
    return await req().ConfigureAwait(false);
  }
  catch (Exception ex)
  {
    // Parse exception here and throw custom exceptions
  }
}

Then your async lambdas are converted to async Task<T> methods instead of async void.

I have more information on sync/async delegates on my blog.


  • Is it necessary to also call ConfigureAwait(false) ? Or is it just an optimization not to switch context back? - Don Box
  • @DonBox: It's just an optimization. - Stephen Cleary

Linked


Related

Latest