0
我有一个流程:WebApi> ServiceFramework> DBLayer> MongoDB。异步链接块webapi调用
由于它是一个新的应用程序,我确保在所有图层中从头开始异步。但是,当我的数据库层具有异步代码时,webapi永远不会得到回应。
API CONTROLLER
[HttpGet]
public IHttpActionResult GetAllRecords()
{
var result = FrameworkApi.GetRecords().Result;
return Ok(result);
}
上述通话>框架API
public async Task<List<Record>> GetRecords()
{
return await FrameworkDbApi.GetRecords();
}
上述通话> DB框架API(其调用的MongoDB)
public async Task<List<Record>> GetRecords()
{
return await Task.Run(() =>
NoSqlDocumentClient.GetDefaultDatabase().Result.
GetCollection<Record>("record").AsQueryable().ToList());
//following Synchronous version works..but defeats the purpose
//return NoSqlDocumentClient.GetDefaultDatabase().Result
// .GetCollection<Record>("record").AsQueryable().ToList();
}
但是,当通过测试用例调用DBLayer或Framework中的操作时,我确实会得到结果。但是当通过WebApi控制器调用时,异步版本永远不会返回响应,而同步版本可以正常工作。