简体   繁体   English

在App_start文件夹中专门配置ninjectwebcommon.cs文件配置Ninject

[英]Configuring Ninject in App_start folder specifically ninjectwebcommon.cs file

I have a simple query, i am following the tutorial on this link: http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier My problem is the author on this tutorial configure ninject in the global.asax file and deleted the ninjectwebcommon.cs file. 我有一个简单的查询,我正在关注此链接上的教程: http : //www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#book-unique-identifier我的问题是该教程的作者configure ninject在global.asax文件中,并删除了ninjectwebcommon.cs文件。 i am trying to integrate the justblog into my existing asp.netMVC5 application that is using the ninjectwebcommon.cs file. 我正在尝试将justblog集成到使用ninjectwebcommon.cs文件的现有asp.netMVC5应用程序中。

Any help would be much appreciated. 任何帮助将非常感激。

Did you use Nuget to add Ninject? 您是否使用Nuget添加Ninject? You'll need a reference to WebActivatorEx for the bootstrapper to work (obviously along with the other required Ninject references). 您需要对WebActivatorEx的引用才能使引导程序正常工作(显然,还要与其他必需的Ninject引用一起使用)。 Add a NinjectWebCommon.cs class in your App_Start folder in your project, looking like this: 在项目的App_Start文件夹中添加一个NinjectWebCommon.cs类,如下所示:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(YourMvcApp.App_Start.NinjectWebCommon), "Stop")]

namespace YourMvcApp.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel(); // you'll add modules to the parameter list here
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                //RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        ///// <summary>
        ///// Load your modules or register your services here!
        ///// </summary>
        ///// <param name="kernel">The kernel.</param>
        //private static void RegisterServices(IKernel kernel)
        //{
        //}
    }
}       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM