2012-04-01 42 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