i've small application (for now) uses translations. translation service called translator (itranslator).
my problem don't want retrieve class in every controller (which returns view) may use in views.
i've similar problem listservice (where store selectlistitem
lists).
customwebviewpage
public abstract class customviewpage<tmodel> : webviewpage<tmodel> { readonly iscriptfactory scriptfactory; readonly imembership membership; readonly ilistservice listservice; readonly itranslator translator; ihtmlstring path; public imembership membership { { return this.membership; } } public override iprincipal user { { return this.membership.user; } } public ilistservice lists { { return this.listservice; } } public itranslator translator { { return this.translator; } } public customviewpage(iscriptfactory scriptfactory, imembership membership, ilistservice listservice, itranslator translator) { this.scriptfactory = scriptfactory; this.membership = membership; this.listservice = listservice; this.translator = translator; } public ihtmlstring ownscriptspath { { return path ?? (path = scriptfactory.create(this.html).tohtmlstring()); } } private string languagehtml = "<meta name=\"language\" content=\"" + membership.currentlanguage + "\">"; public ihtmlstring metalanguage { { return mvchtmlstring.create(languagehtml); } } }
and usage:
<div> @lists.getnationalities(model.nationalityid) </div>
however, when running application i've following error:
'[namespace].customviewpage' not contain constructor takes 0 arguments
but if create second constructor, simple-injector throw error since doesn't support multiple constructors.
how may achieve want, or isn't possible?
edit
full stack trace
c:\windows\microsoft.net\framework\v4.0.30319\temporary asp.net files\vs\520dfaa2\f5e4578c\app_web_index.cshtml.a8d08dba.jtlk7fxz.0.cs(40): error cs1729: 'sportshub.sdk.infrastructure.customviewpage' not contain constructor takes 0 arguments
at system.web.compilation.assemblybuilder.compile() @ system.web.compilation.buildproviderscompiler.performbuild() @ system.web.compilation.buildmanager.compilewebfile(virtualpath virtualpath) @ system.web.compilation.buildmanager.getvpathbuildresultinternal(virtualpath virtualpath, boolean nobuild, boolean allowcrossapp, boolean allowbuildinprecompile, boolean throwifnotfound, boolean ensureisuptodate) @ system.web.compilation.buildmanager.getvpathbuildresultwithnoassert(httpcontext context, virtualpath virtualpath, boolean nobuild, boolean allowcrossapp, boolean allowbuildinprecompile, boolean throwifnotfound, boolean ensureisuptodate) @ system.web.compilation.buildmanager.getvirtualpathobjectfactory(virtualpath virtualpath, httpcontext context, boolean allowcrossapp, boolean throwifnotfound) @ system.web.compilation.buildmanager.getcompiledtype(virtualpath virtualpath) @ system.web.compilation.buildmanager.getcompiledtype(string virtualpath) @ system.web.mvc.buildmanagerwrapper.system.web.mvc.ibuildmanager.getcompiledtype(string virtualpath) @ system.web.mvc.buildmanagercompiledview.render(viewcontext viewcontext, textwriter writer) @ system.web.mvc.viewresultbase.executeresult(controllercontext context) @ system.web.mvc.controlleractioninvoker.invokeactionresult(controllercontext controllercontext, actionresult actionresult) @ system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist`1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) @ system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist`1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) @ system.web.mvc.controlleractioninvoker.invokeactionresultwithfilters(controllercontext controllercontext, ilist`1 filters, actionresult actionresult) @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass21.<>c__displayclass2b.<begininvokeaction>b__1c() @ system.web.mvc.async.asynccontrolleractioninvoker.<>c__displayclass21.<begininvokeaction>b__1e(iasyncresult asyncresult)
edit 2
as @striplingwarrior , @steven suggested, used property injection, doesn't seem working on webviewpage
i tested on service , property injection successful, in class, property comes null
.
ps: made properties public get;set;
new viewpage:
public abstract class customviewpage<tmodel> : webviewpage<tmodel> { [inject] public iscriptfactory scriptfactory { get; set; } [inject] public imembership membership { get; set; } [inject] public ilistservice listservice { get; set; } [inject] public itranslator translator { get; set; } ihtmlstring scriptspath; public ihtmlstring ownscriptspath() { if (scriptspath == null) { scriptspath = scriptfactory.create(this.html).tohtmlstring(); } return scriptspath; } /// <summary> /// renders current user language meta tag /// </summary> public ihtmlstring metalanguage() { return mvchtmlstring.create("<meta name=\"language\" content=\"" + membership.currentlanguage + "\">"); }
for injectattribute
i've followed following documents.
you have use register dependency container this. that's way, can specify interfaces , matching implementation when asp.net call simpleinject resolve dependency.
you can create parameterless constructor this:
public customviewpage() : base(new scriptfactory(), new membership()...) { }
but wouldn't recommend that... best thing it's have dependency resolution logic working.
Comments
Post a Comment