Chazt3n
Chazt3n

Reputation: 1651

FirstOrDefaultAsync() Hangs MVC5

Someone please help me not kill my Server.. Here's my MVC controller action: (Don't worry about names, I'm mid-refactoring)

 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);
      }
    }

Service:

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

Repo:

  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;
}

EDIT

DbContext Module

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) {
    }
  });
}

Bonus points on if you can school me on my includes, they may very well be terrible.. It's pretty slow just grabbing the lead right now. Hence the async change. I'd like to see if it will be easier on the system with more users. (I can do my own profiling/tests on whether explicit loading will be better here, just saying..)

Anyway, I hit this, the server is completely borked when the await FirstOrDefaultAsync() gets hit.

EDIT 2: I've updated the controller action to show exactly what I'm doing here. I only included the code that was being hit originally.

Upvotes: 1

Views: 830

Answers (1)

beautifulcoder
beautifulcoder

Reputation: 11320

Um, are you returning anything in your controller? That would cause it to hang.

Try

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

Upvotes: 1

Related Questions