CsharpDriver 2.2.3 Findasync永远不会返回

时间:2016-03-25 04:15:28

标签: asp.net-mvc-5 mongodb-.net-driver

我想列出我的数据中的所有文档,并使用Findasync(csharpdriver 2.2.3)查找所有文档,但它永远不会返回。你能给我一些建议吗? 这是我的代码

public class HomeController : Controller
    {
        readonly MyVietnamContext Context = new MyVietnamContext();
        private List<UserModels> list = new List<UserModels>();
        public ActionResult Index()
        {
            GetUsers().Wait();
            return View(list);
        }

        public async Task GetUsers()
        {
            var filter = new BsonDocument();
            var collection = Context.Collection();            
            var cursor = await collection.FindAsync(filter);
            while (await cursor.MoveNextAsync())
            {
                var batch = cursor.Current;
                list.AddRange(batch);
            }            
        }
    }

1 个答案:

答案 0 :(得分:0)

将您的代码更改为

public async Task<ActionResult> Index()
{
   await GetUsersAsync();
   return View(list);
}

public async Task<Context.Collection> GetUsersAsync()
{
    var filter = new BsonDocument();
    var collection = Context.Collection();            
    var cursor = await collection.FindAsync(filter);
    while (await cursor.MoveNextAsync())
    {
       var batch = cursor.Current;
       list.AddRange(batch);
    }      
    return list;      
}

还要查看Using Asynchronous Methods in ASP.NET MVC 4页面。