1

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.

Chazt3n
  • 1,641
  • 3
  • 17
  • 42
  • Can you show how your context instantiated/disposed? If you're running multiple queries async from the same context at the same time, EF may not be happy. – ESG Sep 06 '15 at 00:07
  • An instance is actually getting created per request by Autofac, but this request will get fired independently of other lifetime scopes as it's a javascript ajax call Editing the answer to show the code. – Chazt3n Sep 06 '15 at 01:48
  • @Chazt3n: Are you targeting .NET 4.5, and have you set `targetFramework` to `4.5` in your `web.config`? – Stephen Cleary Sep 06 '15 at 01:54
  • @StephenCleary, yes sir, every project in the solution is. – Chazt3n Sep 06 '15 at 02:35
  • 1
    @Chazt3n: In that case, please post a minimal, reproducible code example. – Stephen Cleary Sep 06 '15 at 12:00
  • This may help: http://stackoverflow.com/questions/15444271/should-method-that-get-task-and-passes-it-away-await-it – VahidN Sep 07 '15 at 07:41
  • I only use `await` in the controller code. Try removing your `async/await` keywords from your Service and Repo. – L_7337 Sep 08 '15 at 11:40
  • @L_7337 for real? Don't I have to do async/await all the way down for it to really be async? – Chazt3n Sep 17 '15 at 17:54

1 Answers1

1

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);
}
beautifulcoder
  • 10,832
  • 3
  • 19
  • 29
  • Sorry, yes I am returning a partial view. I didn't realize that was relevant when I posted as it didn't make it that far. (I see how it absolutely can be) – Chazt3n Sep 06 '15 at 02:33