简单注入器-自定义WebViewPage

本文关键字:WebViewPage 自定义 注入器 简单 | 更新日期: 2023-09-27 17:59:27

我(目前)有一个使用翻译的小型应用程序。我的翻译服务叫做翻译(ITranslator)。

我的问题是,我不想在每个控制器(返回一个视图)中检索这个类,所以我可以在视图中使用它。

ListService(我在其中存储所有的SelectListItem列表)也有类似的问题。

自定义WebViewPage

public abstract class CustomViewPage<TModel> : WebViewPage<TModel>
{
    readonly IScriptFactory scriptFactory;
    readonly IMembership membership;
    readonly IListService listService;
    readonly ITranslator translator;
    IHtmlString path;
    public IMembership Membership { get { return this.membership; } }
    public override IPrincipal User { get { return this.membership.User; } }
    public IListService Lists { get { return this.listService; } }
    public ITranslator Translator { get { 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
    {
        get { return path ?? (path = scriptFactory.Create(this.Html).ToHtmlString()); }
    }
    private string languageHtml =
        "<meta name='"language'" content='"" + membership.CurrentLanguage + "'">";
    public IHtmlString MetaLanguage { get { return MvcHtmlString.Create(languageHtml); } }
}

以及用途:

<div>
   @Lists.GetNationalities(Model.NationalityId)
</div>

然而,当运行应用程序时,我会出现以下错误:

'[命名空间]。CustomViewPage"不包含构造函数需要0个参数

但是,如果我创建第二个构造函数,Simple Injector将抛出一个错误,因为它不支持多个构造函数。

我如何才能实现我想要的,或者这不可能吗?

编辑

全堆栈跟踪

c: ''Windows''Microsoft。NET''Framework''v4.0.30119''临时ASP。NET文件''vs''520dfaa2''f5e4578c''App_Web_index.cs.html.a8d08dba.jtlk7fxz.0.cs(40):错误CS1729:"SportsHub。SDK。基础设施自定义视图页面'不包含接受0个参数的构造函数

at System.Web.Compilation.AssemblyBuilder.Compile()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)    at System.Web.Compilation.BuildManager.GetCompiledType(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetCompiledType(String virtualPath)
at System.Web.Mvc.BuildManagerWrapper.System.Web.Mvc.IBuildManager.GetCompiledType(String virtualPath)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)

编辑2

正如@StriplingWarrior和@Steven所建议的,我使用了属性注入,但它似乎对WebViewPage 不起作用

我在一个服务上测试了它,属性注入是成功的,但在这个类中,属性是null

附言:我用get公开了这些房产;设置

新视图页面:

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 the current user language with a meta tag
    /// </summary>
    public IHtmlString MetaLanguage()
    {
        return MvcHtmlString.Create("<meta name='"language'" content='"" + membership.CurrentLanguage + "'">");
    }

对于InjectAttribute,我遵循了以下文档。

简单注入器-自定义WebViewPage

您必须使用这样的寄存器依赖容器。这样,您就可以指定所有的接口,以及当ASP。NET将调用该SimpleInject来解析依赖关系。

您还可以创建这样一个无参数构造函数:

public CustomViewPage() : base(new ScriptFactory(), new Membership()...) { }

但我不建议。。。最好的办法是让这个依赖解析逻辑正常工作。