在 ViewComponent 中:此异步方法缺少 'await' 运算符,将 运行 同步
In ViewComponent: This async method lacks 'await' operators and will run synchronously
在 ViewComponent 中我收到了这个警告:
(我用过ASP.NET Core 2
)
warning CS1998: This async method lacks 'await' operators and will run
synchronously. Consider using the 'await' operator to await
non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work
on a background thread.
我该如何解决?
public class GenericReportViewComponent : ViewComponent
{
public GenericReportViewComponent()
{
}
public async Task<IViewComponentResult> InvokeAsync(GenericReportViewModel model)
{
return View(model);
}
}
更新:
在视图中,我有 @await
:
<div class="container">
@await Component.InvokeAsync("GenericReport", new GenericReportViewModel() { })
</div>
这不需要是异步的,因为您没有做任何可以从异步操作中获益的事情。删除异步和任务<>。
您没有在方法中使用任何异步调用(没有 await
),因此出现警告。 ViewComponent
有 2 个方法 InvokeAsync
和 Invoke
。当实现中没有异步调用时,您应该使用 ViewComponent
的同步版本 (Invoke
):
public class GenericReportViewComponent : ViewComponent
{
public IViewComponentResult Invoke(GenericReportViewModel model)
{
return View(model);
}
}
这是关于同步工作的文档部分:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.2#perform-synchronous-work
您的操作中没有 await
方法 InvokeAsync
您可以安全地删除 async
并将 return 更改为 IViewComponentResult
public IViewComponentResult Invoke(GenericReportViewModel model)
{
return View(model);
}
在 ViewComponent 中我收到了这个警告:
(我用过ASP.NET Core 2
)
warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
我该如何解决?
public class GenericReportViewComponent : ViewComponent
{
public GenericReportViewComponent()
{
}
public async Task<IViewComponentResult> InvokeAsync(GenericReportViewModel model)
{
return View(model);
}
}
更新:
在视图中,我有 @await
:
<div class="container">
@await Component.InvokeAsync("GenericReport", new GenericReportViewModel() { })
</div>
这不需要是异步的,因为您没有做任何可以从异步操作中获益的事情。删除异步和任务<>。
您没有在方法中使用任何异步调用(没有 await
),因此出现警告。 ViewComponent
有 2 个方法 InvokeAsync
和 Invoke
。当实现中没有异步调用时,您应该使用 ViewComponent
的同步版本 (Invoke
):
public class GenericReportViewComponent : ViewComponent
{
public IViewComponentResult Invoke(GenericReportViewModel model)
{
return View(model);
}
}
这是关于同步工作的文档部分:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.2#perform-synchronous-work
您的操作中没有 await
方法 InvokeAsync
您可以安全地删除 async
并将 return 更改为 IViewComponentResult
public IViewComponentResult Invoke(GenericReportViewModel model)
{
return View(model);
}