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
Post a Comment