异步方法的返回类型必须为空

异步方法的返回类型必须为空

本文介绍了如何解决:异步方法的返回类型必须为空,Task或Task< T> [AppName]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS Code,并遵循ASP.NET Core/EF Core教程,并且承认我不清楚async,await和Task的工作方式(我知道前两个,但不知道第三个) .)我是第一次实现存储库,并实现了一个UnitofWork类和接口.这是UnitofWork类:

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

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

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]

其他可能相关的摘要:

using System;
using System.Threading.Tasks;

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

在我的Startup.cs中:

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

推荐答案

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

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

与您的界面相同

这篇关于如何解决:异步方法的返回类型必须为空,Task或Task&lt; T&gt; [AppName]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:28