2011-06-16 71 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

相關問題