【问题标题】:Simple Injector with MVC and WebAPI带有 MVC 和 WebAPI 的简单注入器
【发布时间】:2017-04-10 02:54:51
【问题描述】:

我有一个包含 WebAPI 和 MVC 控制器的 C# 项目,我想使用 Simple Injector。 https://simpleinjector.readthedocs.io/en/latest/mvcintegration.html 的 Simple Injector 文档将 MVC 的 Application_Start 方法显示为

protected void Application_Start(object sender, EventArgs e) {
    // Create the container as usual.
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

    // Register your types, for instance:
    container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);

    // This is an extension method from the integration package.
    container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

    container.Verify();

    DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}

https://simpleinjector.readthedocs.io/en/latest/webapiintegration.html 显示以下 WebAPI

protected void Application_Start() {
    // Create the container as usual.
    var container = new Container();
    container.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();

    // Register your types, for instance using the scoped lifestyle:
    container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);

    // This is an extension method from the integration package.
    container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

    container.Verify();

    GlobalConfiguration.Configuration.DependencyResolver =
    new SimpleInjectorWebApiDependencyResolver(container);

    // Here your usual Web API configuration stuff.
}

请问如何编写 Application_Start 代码,以便在 WebAPI 和 MVC 控制器中使用 Simple Injector?

【问题讨论】:

    标签: asp.net-mvc asp.net-web-api simple-injector


    【解决方案1】:

    你可以像这样把代码放在一起:

    protected void Application_Start(object sender, EventArgs e) {
        var container = new Container();
        container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
    
        container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);
    
        // User MVC integration
        container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
    
        // Use Web API integration
        container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
    
        container.Verify();
    
        // Plug in Simple Injector into MVC
        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
    
        // Plug in Simple Injector into Web API
        GlobalConfiguration.Configuration.DependencyResolver =
            new SimpleInjectorWebApiDependencyResolver(container);
    }
    

    【讨论】:

    • 这给了System.InvalidOperationException was unhandled by user code Message=The configuration is invalid. Creating the instance for type HelpController failed. For the container to be able to create HelpController it should have only one public constructor: it has 2. See https://simpleinjector.org/one-constructor for more information. 我的项目中实际上没有HelpController!?!
    • @Caltor 是的,您的应用程序某处确实有一个 HelpController。只需在项目中搜索即可。它可能由使用的 VS 模板为您生成。
    • 好吧好吧好吧……看看那个!所以我愿意。它在区域/帮助页面中。我忘记了 VS 自动为 WebAPI 创建的帮助页面。感谢那。很遗憾你比我更了解我的项目。
    猜你喜欢
    • 2015-02-15
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多