c# - Autofac integration with Mobile Azure Service -
i have created mobile azure service project , integrated autofac.
here iocconfig.cs
in have registered apicontrollers , libs.
var containerbuilder = new containerbuilder(); //register api controllers containerbuilder.registerapicontrollers(typeof(usercontroller).assembly); /*register libs*/ containerbuilder.registertype<userlib>().as<iuserlib>(); /*register objectcontexts*/ containerbuilder.registertype<mobileservicecontext>() .as<dbcontext>().instanceperdependency(); var container = containerbuilder.build(); // var resolver = new autofacdependencyresolver(container); dependencyresolver.setresolver(new autofacdependencyresolver(container)); //for web api dependency resolver var resolver = new autofacwebapidependencyresolver(container); globalconfiguration.configuration.dependencyresolver = resolver; return container;
but api not calling due issue , throws error controller not registered
.
if put below code, work fine, simple dependency:
var options = new configoptions(); var builder = new configbuilder(options, (httpconfig, autofac) => autofac.registerinstance(new userlib(new mobileservicecontext())).as<iuserlib>()); httpconfiguration config = serviceconfig.initialize(builder);
here webapiconfig , in serviceconfigurations done.
public static class webapiconfig { public static void register() { // use class set configuration options mobile service configoptions options = new configoptions(); // use class set webapi configuration options httpconfiguration config = serviceconfig.initialize(new configbuilder(options)); // display errors in browser during development, uncomment following // line. comment out again when deploy service production use. // config.includeerrordetailpolicy = includeerrordetailpolicy.always; database.setinitializer(new createdatabaseifnotexists<mobileservicecontext>()); //database.setinitializer(new mobileserviceinitializer()); } }
code calls iocconfig method:
protected void application_start() { webapiconfig.register(); iocconfig.registerdependencies(); databaseconfig.initializedatabase(); }
i have changed ioc config related services. working.
public static void registerdependencies() { try { // use class set configuration options mobile service configoptions options = new configoptions(); // use class set webapi configuration options httpconfiguration config = serviceconfig.initialize(new configbuilder(options, (configuration, builder) => { //register api controllers builder.registerapicontrollers(typeof(usercontroller).assembly); /*register libs*/ builder.registertype<userlib>().as<iuserlib>(); /*register objectcontexts*/ builder.registertype<mobileservicecontext>().as<dbcontext>().instanceperdependency(); /*register datarepository here*/ builder.registergeneric(typeof(datarepository<>)).as(typeof(idatarepository<>)); })); } catch (exception ex) { throw ex; } }
Comments
Post a Comment