185

A little background information.

I am learning the Web API stack and I am trying to encapsulate all data in the form of a "Result" object with parameters such as Success and ErrorCodes.

Different methods however, would produce different results and error codes but the result object would generally be instantiated the same way.

To save some time and also to learn more about async/await capabilities in C#, I am trying to wrap all the method bodies of my web API actions in an asynchronous action delegate but got caught in a bit of a snag...

Given the following classes:

public class Result
{
    public bool Success { get; set; }
    public List<int> ErrorCodes{ get; set; }
}

public async Task<Result> GetResultAsync()
{
    return await DoSomethingAsync<Result>(result =>
    {
        // Do something here
        result.Success = true;
        
        if (SomethingIsTrue)
        {
            result.ErrorCodes.Add(404);
            result.Success = false;
        }
    }
}

I want to write a method that performs an action on a Result object and return it. Normally through synchronous methods it would be

public T DoSomethingAsync<T>(Action<T> resultBody) where T : Result, new()
{
    T result = new T();
    resultBody(result);
    return result;
}

But how do I transform this method into an asynchronous method using async/await?

This is what I have tried:

public async Task<T> DoSomethingAsync<T>(Action<T, Task> resultBody) 
    where T: Result, new()
{
    // But I don't know what do do from here.
    // What do I await?
}
Amal K
  • 4,359
  • 2
  • 22
  • 44
Albin Anke
  • 1,895
  • 2
  • 12
  • 6
  • 1
    If you're `new`-ing up the `T`, why does your method need to be asynchronous? AFAIK in code *using* asynchronous APIs, you only need to propagate the `async`ness from other methods you use. – millimoose Dec 17 '13 at 02:14
  • Sorry I'm fairly new to this still, what do you mean when you say you only need to propagate, and what does new-ing the T have to do with it? – Albin Anke Dec 17 '13 at 02:23
  • 1
    Why are you even trying to do this async? More often in not in webserver situations doing fake async by wrapping synchronous code in tasks (like you are trying to do) is ***slower*** than just doing it synchronously. – Scott Chamberlain Dec 17 '13 at 03:19
  • 1
    @AlbinAnke By "propagate" I mean that **if** you're calling a .NET method like `Stream.ReadAsync()` in a method, that method should itself be asynchronous, and return a `Task` where `T` is what you'd have returned were the method synchronous. The idea is that this way, every caller of your method can then "asynchronously wait" (I don't know what a good term for this is) for the underlying `Stream.ReadAsync()` to complete. A metaphor for this you can use is that async is "infectious", and spreads from low-level built-in I/O into other code whose results depend on those of said I/O. – millimoose Dec 17 '13 at 03:58

2 Answers2

403

The async equivalent of Action<T> is Func<T, Task>, so I believe this is what you're looking for:

public async Task<T> DoSomethingAsync<T>(Func<T, Task> resultBody)
    where T : Result, new()
{
  T result = new T();
  await resultBody(result);
  return result;
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • @Stephen Clearly I'm Trying to implement some similar into a MVVM ligth Messenger, Can I implement the same way ? – Juan Pablo Gomez Nov 04 '15 at 02:00
  • @JuanPabloGomez: I'm not familiar with their kind of messaging, but I don't see why it wouldn't work. – Stephen Cleary Nov 04 '15 at 03:09
  • Func is not the async equivalent of Action. Func is a function delegate which expects a return value whereas action doesn't require to have a return value. – DFSFOT Dec 18 '19 at 16:02
  • 8
    @DFSFOT: The async equivalent of a `void` method is a `Task`-returning method; thus, the async equivalent of `Action` is `Func`, and the async equivalent of `Action` is `Func`. More info [here](https://blog.stephencleary.com/2014/02/synchronous-and-asynchronous-delegate.html). – Stephen Cleary Dec 18 '19 at 16:06
  • @StephenCleary You're right, my bad. What do you do when you have an async call in your action that isn't supposed to return anything tho? – DFSFOT Dec 19 '19 at 12:05
  • 2
    @DFSFOT: An async method should return `Task` when it doesn't have a return value. If it uses the `async` keyword, then the actual `Task` instance will be created by a state machine, not the function directly. – Stephen Cleary Dec 19 '19 at 13:57
  • This is almost exactly what I am looking for, you are awesome. Is there a way to do this without initializing the generic parameter? What if the generic parameter is a class with a non-default constructor? Is there a way to make that work as well? – Ege Aydın May 05 '20 at 18:53
  • @EgeAydın: You'll need to get a `T` instance from somewhere. You can pass `default`, which will be `null` for reference types. For anything more complex than `default` or `new T()` you'll need to either use an existing `T` or use a factory. – Stephen Cleary May 05 '20 at 19:03
  • Shouldn't the method signature be as follows? `async Task DoSomethingAsync(Func> resultBody)` – JohnB Jul 08 '22 at 20:09
  • 3
    @JohnB: No; `Func` is an asynchronous method that takes a parameter of type `T` and has no return value. `Func>` is an asynchronous method that takes no parameters and returns a value of type `T`. – Stephen Cleary Jul 08 '22 at 21:33
  • 10 years ago, you are still smarter than I am today. – Greg Gum Jun 02 '23 at 22:41
  • No. Just more experience in a technology that suddenly became popular. – Stephen Cleary Jun 02 '23 at 23:05
-15

So I believe the way to implement this is:

public Task<T> DoSomethingAsync<T>(Action<T> resultBody) where T : Result, new()
{
    return Task<T>.Factory.StartNew(() =>
    {
        T result = new T();
        resultBody(result);
        return result;
    });
}
Albin Anke
  • 1,895
  • 2
  • 12
  • 6