asp.net-mvc - 如何修复 : The return type of an async method must be void, Task 或 Task<T> [AppName]

标签 asp.net-mvc async-await repository-pattern

我正在使用 VS Code 并遵循 ASP.NET Core/EF Core 教程并承认我不太清楚 async、await 和 Task 是如何工作的(好吧,我知道前两个,但不知道第三个.) 我是第一次实现一个存储库,以及一个 UnitofWork 类和接口(interface)。这是 UnitofWork 类:

using System.Threading.Tasks;

namespace vega.Persistence
{
  public class UnitOfWork : IUnitOfWork
  {
    private readonly VegaDbContext context;

    public UnitOfWork(VegaDbContext context)
    {
      this.context = context;
    }

    public async Task CompleteAsync()
    {
      await context.SaveChangesAsync();
    }
  }
}

除了当我将鼠标悬停在 CompleteAsync 操作名称上时 VS-Code intellisense 显示的主题行错误之外,我还得到了这个:

'UnitOfWork.CompleteAsync()': not all code paths return a value [AppName]

其他可能相关的片段:

using System;
using System.Threading.Tasks;

namespace vega.Persistence
{
    public interface IUnitOfWork
    {
        Task CompleteAsync();
    }
}

在我的 Startup.cs 中:

public void ConfigureServices(IServiceCollection services)
{
    // Add Repository and UnitOfWork --scoped (instance persits for life of request),
    // not Transient or Singleton
    services.AddScoped<IVehicleRepository, VehicleRepository>();
    services.AddScoped<IUnitOfWork, UnitOfWork>();
}

最佳答案

您在项目中定义了另一个 vega.Persistence.Task 类型。只需添加命名空间以将 System.Threading.Tasks.Task 更正为方法的返回类型:

public async System.Threading.Tasks.Task CompleteAsync()
{
    await context.SaveChangesAsync();
}

在您的界面中也是如此。

关于asp.net-mvc - 如何修复 : The return type of an async method must be void, Task 或 Task<T> [AppName],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055577/

相关文章:

javascript - knockout MVC,绑定(bind)模型并将对象添加到模型

asp.net - 调查 Web.Config 重复部分原因的步骤

node.js - Nodejs Lodash map 同步请求

c# - 在 Entity framework6 Repository 模式中限制用户只能访问他/她的实体

jQuery 日期选择器 : Prevent the "Today" button from being grayed out

asp.net-mvc - Asp.net Identity 使用什么算法来加密密码?

c# - 如何编写不带 out 参数的异步方法?

javascript - 如何在 react native 组件中延迟运行异步等待?

c# - 客户端查询生成器 (jquery) 到 Entity Framework 查询

c# - ASP.NET MVC5 EF6 : Given multiplicity constraints, 对应的 'User_AppUser_Target' 也必须处于 'Deleted' 状态。存储库模式