简体   繁体   中英

asp.net MVC 4 Ninject OnApplicationStarted()

Hello Im learning the basics of asp.net by making simple blog with this tutorial

http://www.prideparrot.com/blog/archive/2012/12/how_to_create_a_simple_blog_part1#story1-configure-ninject-mvc

And when I want to configure Ninject for MVC project in MvcApplication.cs there is an error "'System.Web.HttpApplication' does not contain a definition for 'OnApplicationStarted'" what Im doing wrong? There is whole code for this:

using Ninject;
using Ninject.Web.Common;
using System.Web.Routing;
using System.Web.Mvc;
using JustBlog.Core;

namespace JustBlog
{
    public class MvcApplication : NinjectHttpApplication
    {
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();

            kernel.Load(new RepositoryModule());
            kernel.Bind<IBlogRepository>().To<BlogRepository>();

            return kernel;
        }

        protected override void OnApplicationStarted()
        {
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            base.OnApplicationStarted();
        }
    }
}

I've got problem in the last line "base.OnApplicationStarted();".

Unfortunately, there's a lot of old information out there on Ninject... There is no reason to use the NinjectHttpApplication method anymore.

Instead, simply install NinjectMVCx where x is the version 3, 4 or 5. This adds a file to App_Start called NinjectWebCommon.cs that you can use to customize your bindings. There is no need for changing your HttpApplication or anything like that.

You would put this code in RegisterServices of NinjectWebCommon.cs

kernel.Load(new RepositoryModule());
kernel.Bind<IBlogRepository>().To<BlogRepository>();

Just use the file which was added by the package into App_Start: NinjectWebCommon.cs

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

namespace SportStore.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();
        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)
    {
        System.Web.Mvc.DependencyResolver.SetResolver(new SportStore.Infrastructure.NinjectDependencyResolver(kernel));
    }        
}

}

Add IDependencyResolver in my code it is in Infrastructure solution folder:

public class NinjectDependencyResolver : IDependencyResolver
{
    private IKernel iKernel;

    public NinjectDependencyResolver(IKernel kernel)
    {
        iKernel = kernel;
        AddBindigs();
    }

    public object GetService(Type serviceType)
    {
        return iKernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return iKernel.GetAll(serviceType);
    }

    private void AddBindigs()
    {
         //Add your bindings here....
        iKernel.Bind<IProductRepository>().To<EFProductRepository>();


        iKernel.Bind<IOrderProcessor>().To<EmailOrderProcessor>().
            WithConstructorArgument("settings", emailSettings);
    }
}

Application_Start:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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