FirstOrDefaultAsync()挂起MVC5

时间:2015-09-05 23:36:35

标签: entity-framework async-await

有人请帮助我不要杀死我的服务器..这是我的MVC控制器操作: (不要担心名字,我在重构中期)

 public async Task<ActionResult> AllByLead(int leadId)
    {
      try
      {
        var lead = await _researchService.GetLeadWithContacts(leadId);
        var contactViewModels = Mapper.Map<Lead, List<ContactViewModel>>(lead);

        contactViewModels.Each(contact => PopulateContactOptions(contact));

        var listViewModel = new ContactListViewModel {Results = contactViewModels};

        return PartialView(listViewModel);
      }
      catch
      {
        return Json(string.Format(Resources.BasicErrorMessageFormat, "Error retrieving Lead Contacts"),
          JsonRequestBehavior.AllowGet);
      }
    }

服务:

public async Task<Lead> GetLeadWithContacts(int leadId)
{
  return await _repository.GetLeadWithContacts(leadId).ConfigureAwait(false);
}

回购:

  public async Task<Lead> GetLeadWithContacts(int leadId)
{
  var leadEntity = await _context.Leads
    .Where(lead => lead.LeadID == leadId)
    //.Include(lead => lead.LeadContactMaps.Select(map => map.Contact.Addresses))
    //.Include(lead => lead.LeadContactMaps.Select(map => map.Contact.PhoneNumbers))
    //.Include(lead => lead.Organizations.Select(business => business.Addresses))
    //.Include(lead => lead.Organizations.Select(business => business.PhoneNumbers))
    .FirstOrDefaultAsync();

  return leadEntity;
}

编辑

DbContext模块

internal class DbContextModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register(ctx => new CRTechEntities()).InstancePerLifetimeScope();
    }
}

JS Ajax Call:

function populateContactList() {
  var leadId = $("#LeadId").val();
  $.ajax({
    url: url + "/Contact/AllByLead/",
    type: "GET",
    data: { 'leadId': leadId },
    success: function(data) {
      $("#contactContainer").html(data);
    },
    error: function(data) {
    }
  });
}

奖励积分如果你可以通过我的包括学习我,他们可能很可怕......现在很快就抓住了领先优势。因此异步变化。我想知道在拥有更多用户的系统上是否会更容易。 (我可以自己进行分析/测试,看看显式加载在这里是否更好,只是说..)

无论如何,我点击这个,当等待FirstOrDefaultAsync()被击中时,服务器完全被堵塞。

编辑2 :我已更新控制器操作,以显示完全我在此处所做的事情。我只包含了最初被点击的代码。

1 个答案:

答案 0 :(得分:1)

嗯,你在控制器里还有什么东西吗?这会导致它挂起。

尝试

public async Task<JsonResult> AllByLead(int leadId)
{
    var lead = await _researchService.GetLeadWithContacts(leadId);
    return Json(lead);
}