gpt4 book ai didi

c# - 为什么 asyncController 无法获取数据

转载 作者:太空狗 更新时间:2023-10-29 21:37:35 27 4
gpt4 key购买 nike

我是 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com