2012-04-01 46 views
4

我創建了一個新的ASP.NET Web API項目。然後我使用nuget來拉Ninject.Web.Common,然後我從here下載並構建Ninject.Web.WebApi。包括在項目中。我添加了一個服務,並通過構造函數注入,設置綁定(調試器顯示的代碼實際上打綁定),但仍然拋出這個錯誤:在我的項目註冊服務中的NinjectWebCommon綁定不適用於WebApi

Error activating IValueService 
No matching bindings are available, and the type is not self-bindable. 
Activation path: 
    2) Injection of dependency IValueService into parameter valueService of constructor of type ValuesController 
    1) Request for ValuesController 

Suggestions: 
    1) Ensure that you have defined a binding for IValueService. 
    2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 
    3) Ensure you have not accidentally created more than one kernel. 
    4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 
    5) If you are using automatic module loading, ensure the search path and filters are correct. 

庫:

  1. Ninject。 DLL
  2. Ninject.Web.Common
  3. Ninject.Web.Webapi
  4. WebActivator

所有ninject庫被標記爲版本3

這裏是代碼:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectWebCommon), "Start")] 
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectWebCommon), "Stop")] 
namespace MvcApplication3.App_Start 
{ 
using System; 
using System.Web; 

using Microsoft.Web.Infrastructure.DynamicModuleHelper; 

using Ninject; 
using Ninject.Web.Common; 

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(); 
     kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

     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) 
    { 

     kernel.Bind<IValueService>().To<ValueService>(); 
    }   
} 

} 

服務:

public interface IValueService 
{ 
    List<string> GetStrings(); 
} 

public class ValueService : IValueService 
{ 
    public List<string> GetStrings() 
    { 
     return new List<string>{"test", "test"}; 
    } 
} 

控制器:

public class ValuesController : ApiController 
{ 
    private readonly IValueService valueService; 

    // GET /api/values 
    public ValuesController(IValueService valueService) 
    { 
     this.valueService = valueService; 
    } 
} 

下載的項目樣本從github工作,但不是當我創建一個新的項目。我錯過了一步嗎?

回答

5

我只是檢查存在與這個擴展名(至少不會接觸到最新的MVC4預覽)沒有問題:

  • 創建一個新的MVC4網絡API項目
  • 的NuGet安裝,包裝Ninject.Web。的WebAPI
  • 變化控制器+添加綁定

您的代碼看起來不錯。其他地方一定有問題。

+0

謝謝,我不知道有一個nuget包。安裝該軟件包是否對該項目執行了其他操作,而手動包含該dll時則無法執行該操作? – 2012-04-01 18:09:23

+0

我不知道你做了什麼。這個軟件包沒有做任何特別的事情。 – 2012-04-01 18:10:29

+2

這怎麼可能「你的代碼看起來不錯,其他地方一定有問題。」被接受的答案是什麼? – 2017-03-02 02:20:18

-1

不要直接在RegisterServices結合,但首先從 的IDependencyResolver連接創建一個類添加綁定在AddBindings

例如

見書臨ASP.NET MVC5從亞當·弗里曼

+1

你準備發佈一個實際的例子嗎?如果是這樣,它就會丟失。圖書參考是一個非現場資源,應該(如果不可避免的話)完成章節/腳註等。 – dakab 2015-11-19 09:27:16