2011-06-16 74 views
0

我从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. 

回答

1

移动你的模块阵列到

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

到RegisterServices并添加

kernel.Load(modules); 
+0

我会再试一次。我做了类似的事情,但我试图弄清楚之前和之前使用ninject mvc 3插件生成的内容之间的区别。 – chobo2 2011-06-17 05:28:35

0

这是两个不同的方法到配置核心。您使用的方法需要修改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方法中加载模块。

+0

@Darin Dimitrov - 其实我已经加载了模块。我做了一点不同。我只是没有显示它,因为我恢复了代码。我可能会转向那个提供的那个。然而,我只是想明白为什么我所做的并不是突如其来的工作。我也试图弄清楚为什么当我转向新的灵魂时,它仍然没有工作。从我发现http://stackoverflow.com/questions/2423862/error-more-than-one-matching-bindings-are-available-when-using-ninject-web-mvc是你必须绑定控制器这是自我。我以前从来没有这样做过。 – chobo2 2011-06-17 15:53:33

+0

对我来说,似乎我有点倒退。一个新版本出来了,现在我必须将控制器绑定到自己(如果这确实解决了我的问题,我还没有尝试过),当我从来没有这样做过。 – chobo2 2011-06-17 15:54:20

+0

@ chobo2,将*控制器绑定到自己*?你能解释一下这是什么意思吗?我不知道我理解你。只要你有一个控制器把一些接口作为构造参数,并且这个接口通过正确的实现向内核注册,你不需要将任何控制器绑定到任何东西。 – 2011-06-17 16:05:24

相关问题