Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单注入器-自定义WebViewPage_C#_Asp.net Mvc_Dependency Injection_Ioc Container_Simple Injector - Fatal编程技术网

C# 简单注入器-自定义WebViewPage

C# 简单注入器-自定义WebViewPage,c#,asp.net-mvc,dependency-injection,ioc-container,simple-injector,C#,Asp.net Mvc,Dependency Injection,Ioc Container,Simple Injector,我有一个使用翻译的小应用程序(目前)。 我的翻译服务叫做翻译(ITranslator) 我的问题是我不想在每个控制器(返回一个视图)中检索这个类,所以我可以在视图中使用它 我在ListService中也遇到了类似的问题(在这里我存储了我所有的SelectListItem列表) CustomWebViewPage public abstract class CustomViewPage<TModel> : WebViewPage<TModel> { readonly

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

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

我在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); } }
}

对于
InjectAttribute
我遵循了以下步骤。

您必须使用类似的寄存器依赖项容器。这样,当ASP.NET调用SimpleInput来解析依赖关系时,您可以指定所有接口以及匹配的实现

您还可以创建如下无参数构造函数:

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

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

这可能与此相关:@StriplingWarrior我已经看到了带有注入属性的示例。但是simple injector没有提供它们,请向我们提供您所获得的异常的完整堆栈跟踪。@LeandroSoares:您不能为SimpleInjector定义一个使用默认构造函数的绑定,然后初始化这些属性吗?@Steven stacktrace添加
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)
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 + "\">");
    }
public CustomViewPage() : base(new ScriptFactory(), new Membership()...) { }