asp.net web api - AutoFac with WebApi, Still looking for parameter less constructor -
i have following class in webapi project , registercontainer() method gets called global.asax class. injecting service in api controller. issue it;s throwing exception , not getting instantiated corrected. says no paremeterless controller found. idea what's wrong code
public class iocconfig { #region public methods , operators /// <summary> /// adds dependencies container /// </summary> /// <returns></returns> public static void registercontainer() { var builder = new containerbuilder(); builder.registercontrollers(assembly.getexecutingassembly()); builder.registerapicontrollers(assembly.getexecutingassembly()); icontainer container = builder.build(); dependencyresolver.setresolver(new autofacdependencyresolver(container)); globalconfiguration.configuration.dependencyresolver = new autofacwebapidependencyresolver(container); registermodules(builder); registercontrollers(builder); } #endregion #region methods private static void registercontrollers(containerbuilder builder) { builder.registerapicontrollers(assembly.getexecutingassembly()); } private static void registerdependencyresolver(icontainer container, containerbuilder builder) { idependencyresolver resolver = new autofacwebapidependencyresolver(container); dependencyresolver.setresolver(resolver); } private static void registermodules(containerbuilder builder) { builder.registermodule(new domainmodule()); builder.registermodule(new repositorymodule()); builder.registermodule(new servicefacademodule()); builder.registermodule(new mappingmodule()); } #endregion }
}
update:
i have changed , added following lines global.asax.
protected void application_start() { arearegistration.registerallareas(); globalconfiguration.configure(webapiconfig.register); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); bundleconfig.registerbundles(bundletable.bundles); //iocconfig.registercontainer(); //webapiconfig.register(globalconfiguration.configuration); var builder = new autofac.containerbuilder(); builder.registercontrollers(typeof(webapiapplication).assembly).propertiesautowired(); builder.registermodule(new repositorymodule()); builder.registermodule(new servicefacademodule()); builder.registermodule(new repositorymodule()); builder.registermodule(new mappingmodule()); builder.registermodule(new domainmodule()); var container = builder.build(); dependencyresolver.setresolver(new autofacdependencyresolver(container)); }
you trying register modules after container has built.
you need move call
registermodules(builder);
before create container builder.build()
:
registermodules(builder); icontainer container = builder.build();
in updated code missing 2 things now:
- registering api controllers
registerapicontrollers
- setting
autofacwebapidependencyresolver
so code should like:
var builder = new autofac.containerbuilder(); builder.registercontrollers(typeof(webapiapplication).assembly).propertiesautowired(); builder.registerapicontrollers(typeof(webapiapplication).assembly).propertiesautowired(); builder.registermodule(new repositorymodule()); builder.registermodule(new servicefacademodule()); builder.registermodule(new repositorymodule()); builder.registermodule(new mappingmodule()); builder.registermodule(new domainmodule()); var container = builder.build(); dependencyresolver.setresolver(new autofacdependencyresolver(container)); globalconfiguration.configuration.dependencyresolver = new autofacwebapidependencyresolver(container);
Comments
Post a Comment