ChatGPT解决这个技术问题 Extra ChatGPT

How do you implement an async action delegate method?

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?
}
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 asyncness from other methods you use.
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?
I think I figured it out, thanks millimoose you gave me something to think about.
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.
@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<T> 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.

S
Stephen Cleary

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 Clearly I'm Trying to implement some similar into a MVVM ligth Messenger, Can I implement the same way ?
This is amazing! I thought it wouldn't be possible to make an async Action, and already considered it a language flaw. I didn't think about using a Func. Thanks.
@DFSFOT: The async equivalent of a void method is a Task-returning method; thus, the async equivalent of Action is Func<Task>, and the async equivalent of Action<T> is Func<T, Task>. More info here.
@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.
@JohnB: No; Func<T, Task> is an asynchronous method that takes a parameter of type T and has no return value. Func<Task<T>> is an asynchronous method that takes no parameters and returns a value of type T.
A
Albin Anke

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;
    });
}

You should avoid Task.Run (and even more so StartNew) on ASP.NET.
What is a better way to do this?
I posted an answer, and upvoted @svick's answer too. They're both good answers.