gpt4 book ai didi

c# - 简单注入(inject)器 - 自定义 WebViewPage

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:03 25 4
gpt4 key购买 nike

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

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

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

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 { 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>

但是,在运行应用程序时出现以下错误:

'[NAMESPACE].CustomViewPage' does not contain a constructor that takes 0 arguments

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

我怎样才能达到我想要的,或者不可能?

编辑

完整的堆栈跟踪

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' does not contain a constructor that takes 0 arguments

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 所建议的,我使用了属性注入(inject),但它似乎不适用于 WebViewPage

我在服务上对其进行了测试,属性注入(inject)成功,但在此类中,该属性为 null

PS:我使用 get;set; 公开了这些属性

新建 View 页面:

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,我遵循了以下 documents .

最佳答案

你必须使用像this 这样的注册依赖容器。 .这样,当 ASP.NET 调用 SimpleInject 来解决依赖关系时,您可以指定所有接口(interface)以及匹配的实现是什么。

你也可以像这样创建一个无参数的构造函数:

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

但我不建议...让这种依赖关系解析逻辑正常工作是最好的事情。

关于c# - 简单注入(inject)器 - 自定义 WebViewPage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38704650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com