以下Web API端点仍然出现以下错误:
“在此前提下开始第二次操作 异步操作完成。“
由于我使用.ToListAsync
和实体框架返回的数据,我假设问题是var user
行阻塞。
如何使该部分同步?
[Route("Retailers/{search}")]
[HttpGet]
public Task<List<Lookup>> Retailers(string search)
{
var user = UserManager.FindById(User.Identity.GetUserId());
Guid userId = Guid.Parse(user.Id);
var companies = _unitOfWork.GetRepository<Company>().GetAll().Where(c => c.CompanyType == CompanyType.Retail &&
(c.UserID == null || c.UserID == userId) && c.CompanyName.StartsWith(search)).Take(5)
.Select(x => new Lookup { Id = x.Id, Name = x.CompanyName }).ToListAsync();
return companies;
}
答案 0 :(得分:2)
您遗漏了async
和await
个关键字。
async
添加到方法签名。await
添加到您对_unitOfWork.GetRepository...ToListAsync()
代码:
[Route("Retailers/{search}")]
[HttpGet]
public async Task<List<Lookup>> Retailers(string search)
{
var user = UserManager.FindById(User.Identity.GetUserId());
Guid userId = Guid.Parse(user.Id);
var companies = await _unitOfWork.GetRepository<Company>().GetAll().Where(c => c.CompanyType == CompanyType.Retail &&
(c.UserID == null || c.UserID == userId) && c.CompanyName.StartsWith(search)).Take(5)
.Select(x => new Lookup { Id = x.Id, Name = x.CompanyName }).ToListAsync();
return companies;
}
作为参考,这里是异步编程的一个很好的链接:Asynchronous Programming with Async and Await。