nam
nam

Reputation: 23868

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

Upvotes: 8

Views: 5454

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 457057

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).

Upvotes: 17

Related Questions