c# - c# ASP.NET MVC 4“此时无法启动异步操作”而没有发生任何异步任务
问题描述
我有一个 MVC 应用程序,在控制器中我收到以下错误:
问题是内部没有发生任何异步,但我刚刚发现是什么代码导致了这种情况并且让我很头疼:
public ActionResult Index()
{
List<Roles> roles = new List<Roles>();
roles = _service.GetRolesByCompany(); // this is syncronous task returning a List<Roles>
if (roles != null && roles.Count > 0)
{
message = "All good";
}
else
{
message = "Missing Settings";
}
return View(message);
}
如果由于某种原因我删除了条件:roles.Count > 0
那么错误就消失了,一切正常。
roles
财产已经解决,不是待解决的事情。这是该方法的代码GetRolesByCompany
:
public List<Roles> GetRolesByCompany() {
List<Roles> roles= null;
using (var conn = new SqlConnection(connString))
{
conn.Open();
try
{
roles = conn.Query<Roles>("GetRolesByCompany",
null,
commandTimeout: 0,
commandType: CommandType.StoredProcedure).ToList();
}
catch (Exception ex) {
}
}
return roles;
}
如果您看到该方法执行 a.ToList()
并且这将解析列表。所以没有什么异步可以解释这个错误。
从今天早上开始,这让我很头疼,任何线索为什么会发生这种情况?
解决方案
推荐阅读
- mysql - 将特定字段插入到 mysql 表中
- google-chrome - 当您单击文本框时,Chrome 72 禁用管理密码对话框
- c - cflow 是否从 C 程序生成声音调用图?
- r - 如何找到相似字符串的行号
- c - 获取协议中方法块的 NSMethodSignature
- android - 尽管已声明,但在清单中找不到类
- javascript - 如何加载 pdf.worker.js
- dependency-injection - Play and Guice Dependency Injection with Interface
- rcpp - 通过 Rcpp 生成对 R_GetCCallable 的调用
- android - Ionic v3 - 仅在打开应用程序时 Android 推送通知颜色而不是图标