Asp.net mvc 使用这两种方法有什么区别?

Asp.net mvc 使用这两种方法有什么区别?,asp.net-mvc,asp.net-mvc-3,ninject,ninject-2,Asp.net Mvc,Asp.net Mvc 3,Ninject,Ninject 2,我从ninject 2.0升级到了2.2,现在什么都不能用了 当我使用nuget时,它会 [assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectMVC3), "Start")] [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.Ninjec

我从ninject 2.0升级到了2.2,现在什么都不能用了

当我使用nuget时,它会

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectMVC3), "Stop")]

namespace MvcApplication3.App_Start
{
    using System.Reflection;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Mvc;

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

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
            DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
            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();
            RegisterServices(kernel);
            return kernel;
        }

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

        }        
    }
}


I use this






     /// <summary>
            /// Application_Start
            /// </summary>
            protected void Application_Start()
            {

                // Hook our DI stuff when application starts
                IKernel kernel = SetupDependencyInjection();

            }


            public IKernel SetupDependencyInjection()
            {
                IKernel kernel = CreateKernel();
                // Tell ASP.NET MVC 3 to use our Ninject DI Container
                DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

                return kernel;
            }

            protected IKernel CreateKernel()
            {
                var modules = new INinjectModule[]
                                  {
                                     new NhibernateModule(),
                                     new ServiceModule(),
                                     new RepoModule()
                                  };


  public class NinjectDependencyResolver : IDependencyResolver
    {
        private readonly IResolutionRoot resolutionRoot;

        public NinjectDependencyResolver(IResolutionRoot kernel)
        {
            resolutionRoot = kernel;
        }

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

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

将模块阵列移动到

var modules = new INinjectModule[]
                                  {
                                     new NhibernateModule(),
                                     new ServiceModule(),
                                     new RepoModule()
                                  };
进入RegisterServices并添加

kernel.Load(modules);

这是配置内核的两种不同方法。您使用的方法需要修改
Global.asax
。NuGet包使用ASP.NET 4的这一新功能,允许在应用程序启动时注册动态模块。由于NuGet包的作者不想因为可能存在其他一些现有代码而弄乱
Global.asax
,因此他们更愿意使用这个单独的文件来配置内核

这两种方法不兼容,您不应该在同一个应用程序中同时使用它们。新版本还包含一个
NinjectDependencyResolver
,因此您不再需要编写或设置任何自定义
DependencyResolver.SetResolver

您只需使用bootstrapper类中的
RegisterServices
静态方法来配置内核:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<ISomeControllerDependency>().To<SomeConcreteImpl>();
}        
就这样。不要忘记从您的
Global.asax
中删除任何NInject跟踪,以避免任何冲突


我想您的代码不能使用第一种方法的原因是您没有在
RegisterServices
方法中加载模块。

我希望您知道,有一个文档可以解释这个问题。

我会再试一次。我做了一些类似的事情,但我正在试图弄清楚我以前做过的和用ninject mvc 3插件生成的有什么区别。@Darin Dimitrov-实际上我已经加载了这些模块。我做的有点不同。我只是在还原代码时没有在那里显示它。我可以移动到提供的那个。然而,我只是想弄明白为什么我所拥有的一切不会突然起作用。我还试图弄明白为什么当我搬到新的soultion时,它仍然不起作用。从我发现的是,你必须把控制器绑定到它的自身。我以前从来没有这样做过,所以对我来说,我似乎有点倒退了。一个新版本已经问世,现在我必须将控制器绑定到它们自己(如果这确实解决了我的问题,我还没有尝试过),而我以前从未这样做过。@chobo2,将控制器绑定到它们自己?你能解释一下这是什么意思吗?我不确定我是否理解你。只要你有一个控制器,它将某个接口作为构造函数参数,并且这个接口是用一个正确的实现在内核中注册的,你就不需要将任何控制器绑定到任何东西上。@Darin Dimitrov-从我发布的链接中读取接受的答案。我和他犯了同样的错误。当我浏览调试器时,我有一些东西需要绑定,并且它绑定正确。然后我去尝试加载一个控制器,它突然说它找不到没有参数的构造函数。当然,升级前一切都正常。@chobo2,你能给我发一个示例项目来说明这个问题吗?我想这对我来说是最容易理解你的情况的,你想做什么,什么不起作用。
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<ISomeControllerDependency>().To<SomeConcreteImpl>();
}        
private static void RegisterServices(IKernel kernel)
{
    kernel.Load(
        new NhibernateModule(),
        new ServiceModule(),
        new RepoModule()
    );
}