asp.net MVC 4 Ninject OnApplicationStarted()

时间:2014-08-10 16:44:38

标签: c# asp.net ninject blogs

您好我通过本教程制作简单的博客来学习asp.net的基础知识

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

当我想在MvcApplication.cs中为MVC项目配置Ninject时,有一个错误“'System.Web.HttpApplication'不包含'OnApplicationStarted'的定义”我做错了什么?有完整的代码:

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();
        }
    }
}

我在最后一行“base.OnApplicationStarted();”中遇到了问题。

2 个答案:

答案 0 :(得分:3)

不幸的是,Ninject上有很多旧信息......没有理由再使用NinjectHttpApplication方法了。

相反,只需安装NinjectMVCx,其中x是版本3,4或5.这会向App_Start添加一个名为NinjectWebCommon.cs的文件,您可以使用该文件自定义绑定。无需更改您的HttpApplication或类似的东西。

您可以将此代码放在NinjectWebCommon.cs的RegisterServices中

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

答案 1 :(得分:1)

只需将包中添加的文件用于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));
    }        
}

}

在我的代码中添加IDependencyResolver,它位于Infrastructure solution文件夹中:

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