首页 > 解决方案 > 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()并且这将解析列表。所以没有什么异步可以解释这个错误。

从今天早上开始,这让我很头疼,任何线索为什么会发生这种情况?

标签: c#asp.net-mvc

解决方案


推荐阅读