2014-04-29 48 views
11

我知道有幾個問題有點像這一個,但由於我無法找到任何文檔,並沒有任何其他問題有任何幫助我的答案,在這裏:使用最新的MVC 5/Web Api 2獲取最新的Ninject?

我創建一個新的ASP。 NET應用程序(VS2013),選擇MVC,我添加API。我在Package Console中運行「update-package」以更新到最新版本(MVC 5.1.2,Web Api 5.1.2)。

然後我添加新的Ninject.MVC5Ninject.Web.WebApi包。

我添加這些行到web.config文件(Ninject想要版本5.0,我有5.1):

 <dependentAssembly> 
      <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" /> 
      <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" /> 
     </dependentAssembly> 
     <dependentAssembly> 
      <assemblyIdentity name="System.Web.Http.WebHost" publicKeyToken="31bf3856ad364e35" /> 
      <bindingRedirect oldVersion="1.0.0.0-5.1.0.0" newVersion="5.1.0.0" /> 
     </dependentAssembly> 

在App_start/NinjectWebCommon.cs文件I添加的單一結合:

private static void RegisterServices(IKernel kernel) { 
     kernel.Bind<ILogger, NLogLogger>(); 
    } 

然後我添加一個ApiController,ILogger作爲構造函數中的單個參數,並且將ILogger作爲參數添加到HomeController的構造函數中。

這應該是測試?

我錯過了WebApi的一些接線?或者這是否發生在幕後?

反正acessing的API控制器給了我這個錯誤:

An error occurred when trying to create a controller of type 'TestController'. Make sure that the controller has a parameterless public constructor.

,並試圖HomeController的給了我這樣的:

Object reference not set to an instance of an object.

我試過同樣的事情,沒有更新到最新的軟件包, 同樣的問題。我一直在使用Ninject多年來一直使用MVC 3(我也嘗試過Ninject.MVC3包),並且完全不知道爲什麼它不起作用。隨着WebApi,我有點期待一些電話SetResolver,但..

任何幫助appriciated!

回答

10

這不是一個MVC /的WebAPI問題

你的配置是錯誤的:

kernel.Bind<ILogger, NLogLogger>(); 

應該

kernel.Bind<ILogger>().To<NLogLogger>(); 
+0

這就是當你與Ninject合作多年時發生的情況,但是在兩者之間有一個抽象!萬分感謝!只是爲了澄清,你的意思是「註冊一次ILogger」是我註冊一次,然後它對MVC和WebApi都有效?至少,似乎發生了什麼。 –

+1

@Remo Gloor:你的答案有語法錯誤。請在'kernel.Bind '之後添加'()'。所以完整的答案代碼將是:'kernel.Bind ()。到();'。我厭倦了改進這個答案,但顯然評論傢伙沒有發現任何有用的糾正語法錯誤! –

13

正如documentation所述,Ninject.Web.WebApi不是獨立的Nuget包。 爲您的應用程序選擇正確的Ninject.Web.WebApi。*包。你的情況很可能是Ninject.Web.WebApi.WebHost。

  1. 創建空的Web應用程序
  2. 安裝Ninject.MVC5
  3. 安裝Ninject.Web.WebApi。虛擬主機提供商
  4. 更新所有的Nu​​Get包
  5. 創建MVC模式控制器和ApiController是採取ILogger依賴
  6. 註冊ILogger一次都MVC和的WebAPI
  7. 運行
+0

謝謝你的鏈接,不知道爲什麼谷歌沒有找到。我已經完成了你寫的內容(並且將這些內容添加到了web.config文件中,就像我在這個問題中寫的那樣),但仍然會出現相同的錯誤。當你寫一次註冊一次ILOGGER時,這是什麼意思?我無法找到任何有關連接WebApi部分的信息。 (我的基礎項目在這裏:https://bitbucket.org/steentottrup/mvc5webapi2ninjecttest) –

+0

感謝雷莫,這個答案是直接的,併爲我工作,而我已經嘗試了很多谷歌的答案。 僅供參考,我的項目使用MVC5和Web API。 – Alex

3

對於在項目中使用Ninject與MVCWebApi你必須安裝下列軟件包:

-Ninject 
-Ninject.MVC5 
-Ninject.Web 
-Ninject.Web.Common 
-Ninject.Web.Common.WebHost 
-Ninject.Web.WebApi 
-Ninject.Web.WebApi.WebHost 

這些軟件包以下文件添加到App_Start文件夾:
NinjectWeb.cs:

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Smartiz.UI.NinjectWeb), "Start")] 

namespace Smartiz.UI 
{ 
    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 

    using Ninject.Web; 

    public static class NinjectWeb 
    { 
     /// <summary> 
     /// Starts the application 
     /// </summary> 
     public static void Start() 
     { 
      DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
     } 
    } 
} 

NinjectWebCommon.cs:

using System; 
using System.Web; 
using Microsoft.Web.Infrastructure.DynamicModuleHelper; 
using Ninject; 
using Ninject.Extensions.Conventions; 
using Ninject.Web.Common; 
using Smartiz.ClientServices; 
using Smartiz.UI; 
using WebActivatorEx; 

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")] 
[assembly: ApplicationShutdownMethod(typeof(NinjectWebCommon), "Stop")] 

namespace Smartiz.UI 
{ 
    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) 
     { 
      //kernel.Bind(q => 
      // q.FromAssembliesMatching("Smartiz.ClientServices*") 
      // .SelectAllClasses() 
      // .InheritedFrom(typeof(BaseService)) 
      // .BindDefaultInterface() 
      // .Configure(c => c.InSingletonScope())); 
     }   
    } 
} 
+0

這不適用於WebApi。 –

+0

爲什麼添加這條線兩次? DynamicModuleUtility.RegisterModule(typeof運算(NinjectHttpModule)); – usefulBee