c# - The type String cannot be constructed. You must configure the container to supply this value -
i have looked @ several tutorials , stackoverflow questions on how implement unity mvc 5 project, can't seem past error :
the type string cannot constructed. must configure container supply value.
i installed nuget package unity.mvc5, , registered type in unity config. have called register components method in unityconfig file.
public static class unityconfig { public static void registercomponents() { var container = new unitycontainer(); // register components container here // not necessary register controllers // e.g. container.registertype<itestservice, testservice>(); container.registertype<istateservice, stateservice>(); dependencyresolver.setresolver(new unitydependencyresolver(container)); } } protected void application_start() { arearegistration.registerallareas(); routeconfig.registerroutes(routetable.routes); unityconfig.registercomponents(); } public class statecontroller : controller { private readonly istateservice stateservice; public statecontroller(istateservice stateservice) { this.stateservice = stateservice; } // get: /state/ public actionresult index() { return view(stateservice.getstates()); } } public interface istateservice : idisposable { ienumerable<state> getstates(); } public class stateservice : istateservice { private myappdbcontext context; public stateservice() : this(new myappdbcontext()){} public stateservice(myappdbcontext context) { this.context = context; } private iqueryable<state> all() { return context.states; } public ienumerable<state> getstates() { return this.all().tolist(); } public void dispose() { context.dispose(); } }
here stack trace:
[invalidoperationexception: type string cannot constructed. must configure container supply value.] microsoft.practices.objectbuilder2.dynamicmethodconstructorstrategy.guardtypeisnonprimitive(ibuildercontext context) +311 microsoft.practices.objectbuilder2.dynamicmethodconstructorstrategy.prebuildup(ibuildercontext context) +229 microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context) +274 microsoft.practices.objectbuilder2.dynamicmethodbuildplancreatorpolicy.createplan(ibuildercontext context, namedtypebuildkey buildkey) +162 microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context) +245 microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context) +274 microsoft.practices.objectbuilder2.buildercontext.newbuildup(namedtypebuildkey newbuildkey) +250 microsoft.practices.unity.objectbuilder.namedtypedependencyresolverpolicy.resolve(ibuildercontext context) +101 lambda_method(closure , ibuildercontext ) +215 microsoft.practices.objectbuilder2.c__displayclass1.b__0(ibuildercontext context) +71 microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context) +42 microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context) +333 microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context) +274 microsoft.practices.objectbuilder2.buildercontext.newbuildup(namedtypebuildkey newbuildkey) +250 microsoft.practices.unity.objectbuilder.namedtypedependencyresolverpolicy.resolve(ibuildercontext context) +101 lambda_method(closure , ibuildercontext ) +205 microsoft.practices.objectbuilder2.c__displayclass1.b__0(ibuildercontext context) +71 microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context) +42 microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context) +333 microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context) +274 microsoft.practices.objectbuilder2.buildercontext.newbuildup(namedtypebuildkey newbuildkey) +250 microsoft.practices.unity.objectbuilder.namedtypedependencyresolverpolicy.resolve(ibuildercontext context) +101 lambda_method(closure , ibuildercontext ) +209 microsoft.practices.objectbuilder2.c__displayclass1.b__0(ibuildercontext context) +71 microsoft.practices.objectbuilder2.dynamicmethodbuildplan.buildup(ibuildercontext context) +42 microsoft.practices.objectbuilder2.buildplanstrategy.prebuildup(ibuildercontext context) +333 microsoft.practices.objectbuilder2.strategychain.executebuildup(ibuildercontext context) +274 microsoft.practices.unity.unitycontainer.dobuildup(type t, object existing, string name, ienumerable`1 resolveroverrides) +383 [resolutionfailedexception: resolution of dependency failed, type = "myapp.mvc.controllers.statecontroller", name = "(none)". exception occurred while: while resolving. exception is: invalidoperationexception - type string cannot constructed. must configure container supply value. ----------------------------------------------- @ time of exception, container was: resolving myapp.mvc.controllers.statecontroller,(none) resolving parameter "stateservice" of constructor myapp.mvc.controllers.statecontroller(myapp.service.services.istateservice stateservice) resolving myapp.service.services.stateservice,(none) (mapped myapp.service.services.istateservice, (none)) resolving parameter "context" of constructor myapp.service.services.stateservice(myapp.data.dal.myappdbcontext context) resolving myapp.data.dal.myappdbcontext,(none) resolving parameter "connectionstring" of constructor myapp.data.dal.myappdbcontext(system.string connectionstring, system.data.entity.infrastructure.dbcompiledmodel model) resolving system.string,(none) ] microsoft.practices.unity.unitycontainer.dobuildup(type t, object existing, string name, ienumerable`1 resolveroverrides) +446 microsoft.practices.unity.unitycontainer.dobuildup(type t, string name, ienumerable`1 resolveroverrides) +50 microsoft.practices.unity.unitycontainer.resolve(type t, string name, resolveroverride[] resolveroverrides) +48 microsoft.practices.unity.unitycontainerextensions.resolve(iunitycontainer container, type t, resolveroverride[] overrides) +61 unity.mvc5.unitydependencyresolver.getservice(type servicetype) +140 system.web.mvc.defaultcontrolleractivator.create(requestcontext requestcontext, type controllertype) +87 [invalidoperationexception: error occurred when trying create controller of type 'myapp.mvc.controllers.statecontroller'. make sure controller has parameterless public constructor.] system.web.mvc.defaultcontrolleractivator.create(requestcontext requestcontext, type controllertype) +247 system.web.mvc.defaultcontrollerfactory.getcontrollerinstance(requestcontext requestcontext, type controllertype) +438 system.web.mvc.defaultcontrollerfactory.createcontroller(requestcontext requestcontext, string controllername) +257 system.web.mvc.mvchandler.processrequestinit(httpcontextbase httpcontext, icontroller& controller, icontrollerfactory& factory) +326 system.web.mvc.mvchandler.beginprocessrequest(httpcontextbase httpcontext, asynccallback callback, object state) +157 system.web.mvc.mvchandler.beginprocessrequest(httpcontext httpcontext, asynccallback callback, object state) +88 system.web.mvc.mvchandler.system.web.ihttpasynchandler.beginprocessrequest(httpcontext context, asynccallback cb, object extradata) +50 system.web.callhandlerexecutionstep.system.web.httpapplication.iexecutionstep.execute() +301 system.web.httpapplication.executestep(iexecutionstep step, boolean& completedsynchronously) +155
what might doing wrong?
update
i added following code registercomponents method in unityconfig class , seems working:
container.registerinstance<stateservice>(new stateservice());
none of tutorials show need this, , feel example case i'm using pretty standard. why need line? don't know unity yet, why still curious.
your stateservice trying resolve "context". recommend keep context out of di mappings keep out of constructors want resolve.
resolving parameter "context" of constructor myapp.service.services.stateservice(myapp.data.dal.myappdbcontext context)
Comments
Post a Comment