Search code examples
c#asp.net-coreasp.net-core-viewcomponent

ViewComponent and InvokeAsync method


In the following method I'm getting the warning: This async method lacks 'await' operators and will run synchronously. Where can I use await in this mehod? Note: This method is returning a simple static View without interacting with a Database etc.

public class TestViewComponent : ViewComponent
{
    public async Task<IViewComponentResult> InvokeAsync()
    {
        return View();
    }
}

Solution

  • Since you have no asynchronous work to do, you could remove the async qualifier and just return Task.FromResult:

    public Task<IViewComponentResult> InvokeAsync()
    {
      return Task.FromResult<IViewComponentResult>(View());
    }
    

    Alternatively, you could just ignore the warning (i.e., turn it off with a #pragma).