简体   繁体   English

异步方法的返回类型

[英]Return Type of an async method

I have an async method in C# as follows: 我在C#中有一个异步方法,如下所示:

public async Task<string> GetData(int id)
        {
        Task<string> inp =  CommonMethod(id);
        return inp;
}

  public async Task<string> CommonMethod(int id)
        {
        string output ;
        output = await service.GetSomeDummyData(id);
        return output;
}

I am getting an error message as "Since this is an async method,the return expression must be of type string rather than 'Task' 我收到一条错误消息,因为“由于这是一个异步方法,所以返回表达式必须是字符串类型,而不是'Task'类型

SO, I converted the return type as : 所以,我将返回类型转换为:

public async Task<string> GetData(int id)
        {
        Task<string> inp =  CommonMethod(id);
        return inp.ToString();
}

The code is getting complied successfully . 该代码已成功编译。

I am new to asynchronous programming, is the above conversion method is a best practice of returning string ? 我是异步编程的新手,以上转换方法是返回字符串的最佳实践吗? Because from the CommonMethod , I am collecting the return type in Task<string> , and in the next statement I am using inp.Tostring() . 因为从CommonMethod中 ,我将在Task<string>收集返回类型,而在下inp.Tostring()语句中,我将使用inp.Tostring() Little bit of Dilemma in using the correct return types. 使用正确的返回类型有点困难。

When you add the async modifier to the method, you're telling the compiler to automatically wrap its return value as a Task. 当您将async修饰符添加到方法中时,您是在告诉编译器自动将其返回值包装为Task。 Or a sequence of Tasks that continue each other on each await statement, loosely speaking. 或者说,一系列任务在每个await语句上彼此连续,松散地说。 So your code: 所以你的代码:

public async Task<string> GetData(int id)
{
    Task<string> inp =  CommonMethod(id);
    return inp;
}

would compile to something resembling : 会编译成类似以下内容的东西:

public Task<string> GetData(int id)
{
     return Task.Run() => 
     {
         Task<string> inp =  CommonMethod(id);
         return inp; 
     }
}

So what you're returning there isn't a Task<string> , but a Task<Task<string>> - a Task which returns another Task (which then returns string), which doesn't match the method signature. 因此,您要返回的内容不是Task<string> ,而是Task<Task<string>> -一个返回另一个Task(然后返回字符串)的Task,该Task与方法签名不匹配。

So what you want to do is either drop the async modifier and return the internal task directly: 因此,您要做的就是删除async修饰符并直接返回内部任务:

public Task<string> GetData(int id)
{
    Task<string> inp =  CommonMethod(id);
    return inp;
} 

which will work, and return the internal CommonMethod task, or add an internal await statement to return a new Task over the results of the internal task: 它将起作用,并返回内部CommonMethod任务,或者添加内部await语句以在内部任务的结果之上返回一个新Task:

public async Task<string> GetData(int id)
{
    string inp =  await CommonMethod(id);
    return inp;
}

which, again loosely, returns a task immediately when the await statement is reached, with the code after it (the return statement) in a ContinueWith block over the internal task. 它再次宽松地在到达await语句时立即返回任务, 其后的代码( return语句)位于内部任务的ContinueWith块中。 When the internal task completes, the code after await runs, and returns the result as the Result of the original Task that was returned earlier. 内部任务完成后, await代码将运行,并返回结果作为先前返回的原始Task的结果。

You should rather do it this way: 您应该这样做:

public async Task<string> GetData(int id)
{
    string inp = await CommonMethod(id);
    return inp;
}

The same way you do it in your second call. 与您在第二个通话中使用的方法相同。 The returned value will get wrapped up in a Task automatically. 返回的值将自动包装在Task中。

Because you had Task as your return type, and you had packed this in Task manually in your code, it was throwing an error there. 因为您将Task作为返回类型,并且已在代码中手动将其打包在Task中,所以它在此处引发了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM