【问题标题】:Whats the difference between using these 2 methods?使用这两种方法有什么区别?
【发布时间】:2011-06-16 21:45:23
【问题描述】:

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

因此,当我尝试使用我的方式(更改之前的工作方式)时,我现在加载它时会得到一些无参数控制器。

当我使用它们时,我得到了

Error occured: Error activating SomeController
More than one matching bindings are available.
Activation path:
  1) Request for SomeController

Suggestions:
  1) Ensure that you have defined a binding for SomeController only once.

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 ninject ninject-2


    【解决方案1】:

    我希望您知道https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application 有一个文档解释了这个问题。

    【讨论】:

      【解决方案2】:

      将你的模块数组移动到

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

      进入 RegisterServices 并添加

      kernel.Load(modules);
      

      【讨论】:

      • 我会再试一次。我做了类似的事情,但我试图弄清楚我之前的内容与 ninject mvc 3 插件生成的内容之间的区别。
      【解决方案3】:

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

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

      您需要做的就是使用引导程序类中的RegisterServices 静态方法来配置您的内核:

      private static void RegisterServices(IKernel kernel)
      {
          kernel.Bind<ISomeControllerDependency>().To<SomeConcreteImpl>();
      }        
      

      如果您有一些想要加载的 NInject 模块:

      private static void RegisterServices(IKernel kernel)
      {
          kernel.Load(
              new NhibernateModule(),
              new ServiceModule(),
              new RepoModule()
          );
      }        
      

      就是这样。不要忘记从您的Global.asax 中删除任何 NInject 跟踪以避免任何冲突。

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

      【讨论】:

      • @Darin Dimitrov - 实际上我确实加载了模块。我做的有点不同。当我恢复代码时,我只是没有在那里显示它。我可能会转到提供的那个。但是,我只是想了解为什么我所拥有的并没有突然起作用。我还试图弄清楚为什么当我搬到新的灵魂时它仍然没有工作。从我发现stackoverflow.com/questions/2423862/… 来看,您必须将控制器绑定到它的自身。我以前从来没有这样做过。
      • 所以对我来说,我似乎有点倒退了。一个新版本出来了,现在我必须将控制器绑定到自己(如果这确实解决了我的问题,我还没有尝试过),而我以前从来没有这样做过。
      • @chobo2,将控制器绑定到自己?你能解释一下这是什么意思吗?我不确定我是否理解你。只要您的控制器将某个接口作为构造函数参数,并且该接口已在内核中注册并具有正确的实现,您就不需要将任何控制器绑定到任何东西。
      • @Darin Dimitrov - 从我发布的链接中阅读接受的答案。我得到了同样的错误有他。当我通过调试器时,我有一些需要绑定的东西,它正确地绑定了它。然后我去尝试加载一个控制器,它突然说它找不到没有参数的构造函数。当然,预升级一切正常。
      • @chobo2,你能给我发一份说明问题的示例项目吗?我认为这对我来说是最容易理解你的场景,你想要做什么,什么不起作用。
      猜你喜欢
      • 2013-09-08
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 2011-01-10
      • 2016-07-01
      • 2013-08-08
      相关资源
      最近更新 更多