How to fix: The return type of an async method must be void, Task or Task<T> [AppName]

拜拜、爱过 提交于 2019-12-11 03:36:57

问题


I'm using VS Code and following an ASP.NET Core/ EF Core tutorial and admit I'm not quite clear on how the async, await and Task do (well, I know the first two, but not the third.) I am implementing a repository for the first time, and a UnitofWork Class and Interface to go with it. Here is the UnitofWork Class:

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

In addition to the subject-line error shown by VS-Code intellisense when I hover over the CompleteAsync action name, I get this:

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

Other perhaps relevant snippets:

using System;
using System.Threading.Tasks;

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

In my 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>();
}

回答1:


You have another vega.Persistence.Task type defined in your project. Just add the namespace to correct System.Threading.Tasks.Task as return type of your method:

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

And same in your interface.



来源:https://stackoverflow.com/questions/44055577/how-to-fix-the-return-type-of-an-async-method-must-be-void-task-or-taskt-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!