ViewComponent and InvokeAsync method

ぃ、小莉子 提交于 2019-12-12 11:13:00

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/40007669/viewcomponent-and-invokeasync-method

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