c# - Add Constructor Arguments To Ninject Binding -
i have asp.mvc project using ninject ioc container. have added binding separate class within infrastructure folder this:
public class ninjectcontrollerfactory : defaultcontrollerfactory{ private readonly ikernel _ninjectkernal; public ninjectcontrollerfactory() { _ninjectkernal = new standardkernel(); psccheckcontext m = _ninjectkernal.get<psccheckcontext>(new iparameter[] { new constructorargument("appnamekey", "name of staff application"), new constructorargument("serverlocationnamekey", "location of application server") }); addbindings(); }
so can see trying set 2 parameters web.config in constructor of psccheckcontext. c# application handles access db. addbindings() class maps interface classes concrete implementations expect:
private void addbindings() { ... _ninjectkernal.bind<ipsccheckcontext>().to<psccheckcontext>(); }
the problem having there 2 constructors in psccheckcontext class second 1 takes different parameter:
public class psccheckcontext : ipsccheckcontext { public psccheckcontext(string appnamekey, string serverlocationnamekey); public psccheckcontext(string imgnamekey, string imgflagkey, string serverlocationkeyname);
when try , hit controller ninject injects binding error that:
error activating string. no matching bindings available, , type not self-bindable.
activation path:
3) injection of dependency string parameter imgnamekey of constructor of type psccheckcontext
2) injection of dependency ipsccheckcontext parameter psccheckcontext of constructor of type accountcontroller
1) request accountcontroller
suggestions:
1) ensure have defined binding string.
2) if binding defined in module, ensure module has been loaded kernel.
3) ensure have not accidentally created more 1 kernel.
4) if using constructor arguments, ensure parameter name matches constructors parameter name.
5) if using automatic module loading, ensure search path , filters correct.
even if change binding better (having read http://thebrainyprofessionals.com/2013/05/20/passing-constructor-parameters-in-ninject/)
iparameter appnamekey = new constructorargument("appnamekey", "name of staff application"); iparameter serverlocationnamekey = new constructorargument("serverlocationnamekey", "location of application server"); _ninjectkernal.bind<ipsccheckcontext>().to<psccheckcontext>() .withconstructorargument(appnamekey) .withconstructorargument(serverlocationnamekey);
i still same error. have read various questions:
inject value injected dependency
creating instance using ninject additional parameters in constructor
convention based binding of constructor string arguments ninject
but none seem having problem.
what doing wrong?
i can't remove additional constructor in different application , think being used different system.
here's accountcontroller constructor completeness
public class accountcontroller : basecontroller private readonly ipsccheckcontext _psccheckcontext; public accountcontroller(ipsccheckcontext psccheckcontext) { this._psccheckcontext = psccheckcontext;
the code in question broken. see:
public ninjectcontrollerfactory() { _ninjectkernal = new standardkernel(); psccheckcontext m = _ninjectkernal.get<psccheckcontext>(new iparameter[] { new constructorargument("appnamekey", "name of staff application"), new constructorargument("serverlocationnamekey", "location of application server") }); addbindings(); }
specifically:
psccheckcontext m = _ninjectkernal.get<psccheckcontext>(new iparameter[] { new constructorargument("appnamekey", "name of staff application"), new constructorargument("serverlocationnamekey", "location of application server") });
what's supposed doing? actually does have ninject instanciate 1 pscheckcontext
instance , forget's it. unless ctor of psccheckcontext
has side effects on system (which not programming practice), code not have effect whatsoever (apart consuming cpu time , memory) (=> notice psccheckcontext m
local variable, can't reused later).
i guess meant was:
public ninjectcontrollerfactory() { _ninjectkernal = new standardkernel(); _ninjectkernal.bind<ipsccheckcontext>().to<psccheckcontext>() .withconstructorargument("appnamekey", "name of staff application") .withconstructorargument("serverlocationnamekey", "location of application server"); addbindings(); }
this binds ipsccheckcontext
constructor arguments on should injectable controller.
Comments
Post a Comment