casillas
casillas

Reputation: 16793

The return type of an async method must be void, Task or Task<T> in C#

I have the following method and getting the following error. I wonder how could I able to overcome on this issue.

The return type of an async method must be void, Task or Task

private static async T GoRequest<T>(string url, Dictionary<string, object> parameters, HttpMethod method,string body = "")
            where T : class
{
      // the rest of the code I commented.
      // the following is the return
      var jsonResult = content.Replace(@"\", "").Trim('"');
                    return typeof (T) == typeof (string)
                        ? jsonResult as T
                        : JsonConvert.DeserializeObject<T>(jsonResult);

}

Upvotes: 0

Views: 1182

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726489

The rule is simple: if your "regular" synchronous method would return T, your async method must return Task<T>:

private static async Task<T> GoRequest<T>(string url, Dictionary<string, object> parameters, HttpMethod method,string body = "")
        where T : class

You don't need to change anything else: when your async code returns a T, the compiler makes sure that the result is actually a Task<T>.

Note: Since your async method has no awaits in it, you might as well change it to a "regular", synchronous, method.

Upvotes: 2

krillgar
krillgar

Reputation: 12805

You need to wrap your return type in a Task.

private static async Task<T> GoRequest<T>(string url, Dictionary<string, object> parameters, HttpMethod method, string body = "")
    where T : object

Upvotes: 3

Related Questions