2

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();".

Matthew Jaeger
  • 59
  • 1
  • 11

2 Answers2

3

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>();
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
1

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);
    }
}
Major
  • 5,948
  • 2
  • 45
  • 60
  • 1
    Sorry but it didnt help, I even reinstalled whole package and that also didnt help – Matthew Jaeger Aug 10 '14 at 18:21
  • It is weird it works me. You need to clear out the start function just like mine. The registration is on the NinjectWebCommon.cs file line 1 and 2... What is the error message? – Major Aug 10 '14 at 18:46
  • As I said its System.Web.HttpApplication' does not contain a definition for 'OnApplicationStarted'. But also I dont really know what you mean about clearing the start function and about your code of "applicatioin_start". Its like there's no function "OnApplicationStarted", its underlined by red line in Visual Studio. – Matthew Jaeger Aug 10 '14 at 19:19
  • Sorry I forget to paste the IDependencyResolver implementation code. I will work now just replace your interfaces... – Major Aug 10 '14 at 21:55