asp.net mvc - How to use dependency injection in ashx (Autofac, MVC4) -


public class autocompletecity : ihttphandler {     public iautocompleterepository autocompleterepository { get; set; }      public void processrequest(httpcontext context)     {     } }  public static void registerdependencies() {     var builder = new containerbuilder();      builder.registertype<autocompleterepository>().as<iautocompleterepository>).singleinstance();     icontainer container = builder.build();     dependencyresolver.setresolver(new autofacdependencyresolver(container)); } 

autocompleterepository registered singleinstance, null when calling processrequest.

how can fix this?

your code looks good. suspect missing in propertyinjectionmodule httpmodules setup in web.config. following module setup required autofac work asp.net:

  <add name="containerdisposal" type="autofac.integration.web.containerdisposalmodule, autofac.integration.web"/>   <add name="propertyinjection" type="autofac.integration.web.forms.propertyinjectionmodule, autofac.integration.web"/> 

note: setup asp.net webforms integration described here.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -