简体   繁体   English

异步方法签名错误。 必须返回void,Task或Task <T>

[英]Async method signature error. Must return void, Task or Task<T>

I have an async method. 我有一个异步方法。

private static async Task InsertConnectionStatusIntoAzureDatabaseAsync(Device device)
{
   ...
}

I call it using 我叫它用

await InsertConnectionStatusIntoAzureDatabaseAsync(device).ConfigureAwait(false);

Visual studio will not build saying that an async method must return void, Task or Task<T> Visual Studio不会构建说async method must return void, Task or Task<T>

It also red squiggles the line InsertConnectionStatusIntoAzureDatabaseAsync(device).ConfigureAwait(false); 它也是红色曲线插入行InsertConnectionStatusIntoAzureDatabaseAsync(device).ConfigureAwait(false); saying : 说:

Task does not contain a definition for ConfigureAwait() 任务不包含ConfigureAwait()的定义

The usings at the top of the file are 文件顶部的用法是

using System.Threading.Tasks;
using System.Threading;

The .net framework it targets is 4.6.1 它所针对的.net框架是4.6.1

You almost certainly have another class in your project called Task which is conflicting with the .Net Framework version. 您几乎肯定在您的项目中有另一个名为Task类,该类与.Net Framework版本冲突。 You can check this by going to the definition of the Task type and see where it leads you. 您可以通过转到Task类型的定义来查看它,并查看它引导您的位置。 So either rename your version to something distinct (probably the best option) or use the full namespace: 因此,要么将您的版本重命名为不同的版本(可能是最佳选项),要么使用完整的命名空间:

private static async System.Threading.Tasks.Task InsertConnectionStatusIntoAzureDatabaseAsync()
{
    //snip
}

You can as well use namespace alias and use that in your method signatire like 您也可以使用命名空间别名,并在方法签名中使用它

using task = System.Threading.Tasks;

    private static async task.Task InsertConnectionStatusIntoAzureDatabaseAsync(Device device)
    {
       ...
    }

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

相关问题 异步方法的返回类型必须为空,任务或任务 <T> 在C#中 - The return type of an async method must be void, Task or Task<T> in C# 异步并等待:为什么其签名定义了Task的返回类型的异步方法显式地返回Task? - Async and await: Why doesn't an async method whose signature defines a return type of Task explicitly return a Task? 视图模型中的异步方法:应该返回Task还是void? - Async method in view-model: should this return Task or void? 是否有可能让 C# 异步方法返回类型不是任务<T>还是无效? - Is it possible to have C# async method return type to be something not Task<T> or void? 异步返回任务方法 - Async return Task method 在方法签名中使用async关键字在Web Api端点中返回Task - Using async keyword in method signature to return a Task in Web Api endpoint 如何返回任务 <T> 在异步方法中 - How to return a Task<T> in an async method async/await - 何时返回任务 vs 无效? - async/await - when to return a Task vs void? 如果可以编写我们自己的等待,那么为什么异步方法只能返回Task <T>,Task和void? - If it's possible to write our own awaitables, then why can async methods only return Task<T>, Task and void? 当签名为公共异步Task MethodName时,为什么不必返回Task? - Why is it not necessary to return a Task when the signature is public async Task MethodName?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM