PakiPat
PakiPat

Reputation: 1085

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

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

Upvotes: 3

Views: 8933

Answers (1)

Andrii Litvinov
Andrii Litvinov

Reputation: 13192

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.

Upvotes: 5

Related Questions