c# - Parameterless Controller Constructor Autofac -
i'm having problems mvc4 project frustratingly works fine on 1 computer (vs 2012), not on other (vs 2010).
(i hope conflicting vs versions aren't problem, project came vs2010 computer, did work on 2012, , i'm trying put on 2010.)
i "make sure controller has parameterless public constructor" error think controller has default one:
public class homecontroller : authorizedcontrollerbase { public viewresult index() { return view(); } }
the code throws error in controllerfactory:
public class controllerfactory : defaultcontrollerfactory { private readonly istateprovider _sessionstateprovider; public controllerfactory(istateprovider astateprovider) { _sessionstateprovider = astateprovider; } public override icontroller createcontroller (system.web.routing.requestcontext requestcontext, string controllername) { icontroller controller = base.createcontroller(requestcontext, controllername); if (controller basecontroller) { var basecontroller = (basecontroller)controller; // ***this line*** basecontroller.sessiondata = _sessionstateprovider; } return controller; } }
i saw question similar interface implementer had no public constructor, again, think mine have default one:
public class sessionstateprovider : istateprovider { public object this[string key] { { return httpcontext.current.session[key]; } set { httpcontext.current.session[key] = value; } } public void remove(string key) { httpcontext.current.session.remove(key); } }
based on answers other questions , stacktrace, i'm looking autofac problems. did install-package autofac
, have latest (3.0.2) in project, , stacktrace looks this:
innerexception: autofac.core.dependencyresolutionexception message=an exception thrown while executing resolve operation. see innerexception details. ---> common language runtime detected invalid program. (see inner exception details.) source=autofac stacktrace: @ autofac.core.resolving.resolveoperation.execute(icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.lifetime.lifetimescope.resolvecomponent(icomponentregistration registration, ienumerable`1 parameters) @ autofac.resolutionextensions.tryresolveservice(icomponentcontext context, service service, ienumerable`1 parameters, object& instance) @ autofac.resolutionextensions.resolveoptionalservice(icomponentcontext context, service service, ienumerable`1 parameters) @ autofac.resolutionextensions.resolveoptional(icomponentcontext context, type servicetype, ienumerable`1 parameters) @ autofac.resolutionextensions.resolveoptional(icomponentcontext context, type servicetype) @ autofac.integration.mvc.autofacdependencyresolver.getservice(type servicetype) @ system.web.mvc.defaultcontrollerfactory.defaultcontrolleractivator.create(requestcontext requestcontext, type controllertype) innerexception: system.invalidprogramexception message=common language runtime detected invalid program. source=autofac stacktrace: @ autofac.core.activators.reflection.constructorparameterbinding..ctor(constructorinfo ci, ienumerable`1 availableparameters, icomponentcontext context) @ autofac.core.activators.reflection.reflectionactivator.<>c__displayclass5.<getconstructorbindings>b__4(constructorinfo ci) @ system.linq.enumerable.whereselectarrayiterator`2.movenext() @ system.linq.enumerable.whereenumerableiterator`1.movenext() @ system.linq.buffer`1..ctor(ienumerable`1 source) @ system.linq.enumerable.toarray[tsource](ienumerable`1 source) @ autofac.core.activators.reflection.reflectionactivator.activateinstance(icomponentcontext context, ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.activate(ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.execute() @ autofac.core.resolving.resolveoperation.getorcreateinstance(isharinglifetimescope currentoperationscope, icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.resolvecomponent(icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.registration.externalregistrysource.<>c__displayclass8.<registrationsfor>b__3(icomponentcontext c, ienumerable`1 p) @ autofac.core.activators.delegate.delegateactivator.activateinstance(icomponentcontext context, ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.activate(ienumerable`1 parameters) @ autofac.core.resolving.instancelookup.execute() @ autofac.core.resolving.resolveoperation.getorcreateinstance(isharinglifetimescope currentoperationscope, icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.resolving.resolveoperation.resolvecomponent(icomponentregistration registration, ienumerable`1 parameters) @ autofac.core.resolving.resolveoperation.execute(icomponentregistration registration, ienumerable`1 parameters)
this happens though indeed registering istateprovider this: builder.register(context => new sessionstateprovider()).as<istateprovider>();
in containerbuilder.buildcontainer()
. in global.asax register controllerfactory:
protected void application_start() { container = containerbuilder.buildcontainer(); var autofacdependencyresolver = new autofacdependencyresolver(container); dependencyresolver.setresolver(autofacdependencyresolver); controllerbuilder.current.setcontrollerfactory(new controllerfactory(container.resolve<istateprovider>())); application_start_mvc(); }
i'm not sure next possible steps trying resolve (i'm way out of depth parsing autofac stacktrace), , i'd appreciate help.
hi don't test in vs 2010 because don't have it. in vs 2013 works fine.
public static class dependencyinjection { public static void init() { // create container builder. var builder = new containerbuilder(); //register controllers builder.registercontrollers(assembly.getexecutingassembly()); // register other dependencies. builder.registertype<sessionstateprovider>().as<istateprovider>().instanceperhttprequest(); // build container. var container = builder.build(); //configure asp.net mvc dependency resolver dependencyresolver.setresolver(new autofacdependencyresolver(container)); } } public class sessionstateprovider:istateprovider { public object this[string key] { { return httpcontext.current.session[key]; } set { httpcontext.current.session[key] = value; } } public void remove(string key) { httpcontext.current.session.remove(key); } } public interface istateprovider { object this[string key] { get; set; } void remove(string key); } public class mvcapplication : system.web.httpapplication { protected void application_start() { arearegistration.registerallareas(); webapiconfig.register(globalconfiguration.configuration); filterconfig.registerglobalfilters(globalfilters.filters); routeconfig.registerroutes(routetable.routes); dependencyinjection.init(); bundleconfig.registerbundles(bundletable.bundles); } } public abstract class basecontroller : controller { protected istateprovider sessionstateprovider { get; private set; } public basecontroller() { sessionstateprovider = dependencyresolver.current.getservice<istateprovider>(); } } public class homecontroller : basecontroller { public actionresult index() { sessionstateprovider["1"] = 2; return view(); } }
autofac -> https://www.nuget.org/packages/autofac.mvc4/
Comments
Post a Comment