【发布时间】:2019-01-20 02:49:29
【问题描述】:
我希望这仍然是主题。在这篇文章中,我看到了如何创建await ViewAsync():
Returning a view with async keyword
所以我的考虑是:好吧,我想让我的应用程序使用多线程,让我们创建一个包含ViewAsync 的方法的BaseController:
只是其中的一部分:
public class BaseController : Controller
{
[NonAction]
public virtual async Task<ViewResult> ViewAsync()
{
return await Task.Run(() => this.View(null));
}
[NonAction]
public virtual async Task<ViewResult> ViewAsync(string viewName)
{
return await Task.Run(() => this.View(viewName, this.ViewData.Model));
}
// the other implementations....
}
现在我总是可以在继承类中这样调用:
[HttpGet]
public async Task<IActionResult> DoSomething()
{
// maybe we need to do something here, maybe not
return await ViewAsync(new DoSomethingObject());
}
恕我直言,我的优势/目标是性能,因为我现在总是可以使用多线程。
我的考虑是否正确?
在帖子中,对答案的评论以I wouldn't do this 开头。但是投票/cmets/答案并不多。这种实施的风险或缺点在哪里?也许,为什么Microsoft.AspNetCore.Mvc.Controller 不附带ViewAsync 方法?
【问题讨论】:
-
A
View只是一个连接字符串的类。没有理由让它们成为async,因为它们应该是轻量级的,易于处理。您的示例为此创建了线程池线程,这太过分了
标签: c# asp.net-mvc asp.net-core async-await