我是 AsyncController
的新手,请帮助我为什么我无法使用异步从数据库中获取数据:
public async Task<ActionResult> Index()
{
Task<IEnumerable<Country>> objctry = Task.Factory.StartNew<IEnumerable<Country>>(objrepo.GetCountry);
await Task.WhenAll(objctry);
return View(objctry);
}
如果我将上面的代码更改为:
public ActionResult Index() { var x = objrepo.GetCountry();return View(x); }
它的工作。 请指导我到底哪里做错了
最佳答案
您正在使用 Task.WhenAll
在这种情况下不正确。
使用Task.Run
并重构操作如下
public async Task<ActionResult> Index() {
IEnumerable<Country> objctry = await Task.Run(() => objrepo.GetCountry);
return View(objctry);
}
关于c# - 为什么 asyncController 无法获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49808741/