另一个操作正在等待,异步模块完成

时间:2016-09-07 17:35:38

标签: c# angularjs asp.net-web-api async-await

我正在连接到Quickbooks api,下载员工信息并将其保存到我的本地数据库。我正在使用angularjs,webapi来实现这一目标。我将信息保存到数据库时收到以下错误。我确实看到所有函数都有异步和等待。有人可以帮我解释为什么我会收到这个错误。

错误:

  

' /'中的服务器错误Application.An异步模块或处理程序在异步操作仍处于挂起状态时完成。

问题出现在下面粘贴的代码中:

 var candidate = await CandidateLoginBL.AddCandidateByEmployeeAsync(new CandidateLoginBO()
                        {
                            FirstName = e.GivenName,
                            MiddleName = e.MiddleName,
                            LastName = e.FamilyName
          });
                        }
                    });

完整的流程如下:

js:

QuickbookModule.factory('QuickbookService', ['$http', function ($http) {

    return {
 getQuickbooksSync: function () {
            return $http({
                url: '/api/QuickbookService/syncQuickbooks',
                method: 'GET',
                params: { IdCompany: sessionStorage.CID }
            });
        }

API控制器:

[HttpGet]
        [Route("syncQuickbooks")]
        public async Task<IHttpActionResult> syncQuickbooks(int IdCompany)
        {
          var result = await QuickbooksBL.FullQuickbooksSync(IdCompany);
          return Ok(result);
        }

QuickbooksBL:

     public static async Task<List<IncompleteEmp>> FullQuickbooksSync(int IdCompany)
            {return await SyncronizeEmps(IdCompany); }

        public static async Task<List<IncompleteEmp>> SyncronizeEmps(int companyId)
            {                   
                   ......      
                    List<EmployeeBO> empList = new List<EmployeeBO>();
                    await AddToHumanEfits(companyId, inQBEmpsInfo);                       ....    
                }
                           return IncompleteEmps;
            }


         public static async Task AddToHumanEfits(int companyId, List<EmployeeQbOnlineBO> qbEmpsList)
            {                
                  ....
                    qbEmpsList.ForEach(async e =>
                    {  

                            // Add a record into Candidate Login. 

                      var candidate = await CandidateLoginBL.AddCandidateByEmployeeAsync(new CandidateLoginBO()
                        {
                            FirstName = e.GivenName,
                            MiddleName = e.MiddleName,
                            LastName = e.FamilyName              });
                        }
                    });    
            }

CandidateContactBL:

 public static async Task<CandidateLoginBO> AddCandidateByEmployeeAsync(CandidateLoginBO model)
        {
            return await CandidateLoginDAL.AddCandidateByEmployeeAsync(model);
        }

CandidateContactDAL:

public static async Task<CandidateLoginBO> AddCandidateByEmployeeAsync(CandidateLoginBO model)
        {
            CandidateLoginBO candidate = new CandidateLoginBO();

                candidate = await GetByUserNameAsync(new CandidateLoginBO() { Email = model.Email });                        candidate = await AddEmployeeAsync(model);                   
            return candidate;
        }

1 个答案:

答案 0 :(得分:4)

此类错误通常由async void引起。我在这里看到一个:

qbEmpsList.ForEach(async e =>
{
  ...
});

您可能希望将其变为常规foreach

foreach (var e in qbEmpsList)
{
  ...
}