Asp.net MongoDB ToListAsync()永不返回
本文关键字:返回 net MongoDB ToListAsync Asp | 更新日期: 2024-09-24 23:36:51
我花了几个小时试图调试它,但没有成功。我使用的是带有C#驱动程序的Mongo 3.2。我已经将问题简化为一个仍然显示问题的简化版本。
问题似乎是ToListAsync()永远不会返回。我已经尝试将LoginHelper函数更改为同步,并检查ToListAsync()的值。验证操作的结果返回了正确的文档,并且确实返回了。但不确定为什么这个异步版本不起作用。
这是代码:
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e) {}
[System.Web.Services.WebMethod]
public static async Task<string> LoginUser(string username, string password)
{
string userId = await LoginHelper(username, password);
if (userId != "N/A")
{
//Do some stuff here
}
return userId;
}
public static async Task<string> LoginHelper(string userName, string password)
{
var client = new MongoClient("mongodb://localhost");
var database = client.GetDatabase(_databaseName);
var collection = database.GetCollection<BsonDocument>("Users");
//This is the trouble line
//If I remove this line the code will run fine and I'll see "N/A" returned in the JavaScript that calls LoginUser WebMethod
var documents = await collection.Find(new BsonDocument()).ToListAsync();
//The code never reaches this point, the above await never returns
return "N/A";
}
}
我只是通过将功能转移到WCF web服务中而不是使用web方法来解决这个问题。我不知道为什么这样做,也许ASPX页面中的Webmethods与async不兼容。