c# - ASP.NET Web API 与 ninject 绑定(bind)

标签 c# asp.net-mvc-4 asp.net-web-api wcf-web-api

我刚刚安装了 mvc4 rc 更新,我正在尝试构建一个 api 应用程序,但运气不佳。

我正在使用 ninject 但无法加载我的 Controller 。我不断收到错误

Type 'Api.Controllers.ConsumerController' does not have a default constructor

我对 mvc 和使用注入(inject)非常陌生,所以请多多包涵。

我没有对通过 nuget 创建的默认绑定(bind)做任何特殊的事情

 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<IConsumerRepository>().To<ConsumerRepository>();
    }        
}

我的 Controller 看起来像

   private readonly IConsumerRepository _repository;

    public ConsumerController(IConsumerRepository repository)
    {
        _repository = repository;
    }

    [HttpGet]
    public IQueryable<Consumer> Get(Guid id)
    {
        return _repository.Get(id).AsQueryable();
    }

我需要做什么才能让 api Controller 与 ninject 一起工作?

对不起,如果这是简单的事情

我尝试了迈克尔的建议,但是在将 webcommon.cs 更改为这个之后

  private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        RegisterServices(kernel);
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(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<IConsumerRepository>().To<ConsumerRepository>();
    }

我得到一个错误
var kernel = new StandardKernel();

被称为

Method 'GetFilters' in type 'Ninject.Web.WebApi.Filter.DefaultFilterProvider' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation.

我错过了什么?

最佳答案

我问了Brad Wilson关于这一点,它在 MVC4 RC 中发生了变化。

GlobalConfiguration.Configuration.ServiceResolver 已移至 GlobalConfiguration.Configuration.DependencyResolver

使用此实现为您的 Web Api 创建一个 Ninject DependencyResolver: https://gist.github.com/2417226

NinjectWebCommon.cs中:

// Register Dependencies
RegisterServices(kernel);

// Set Web API Resolver
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

关于c# - ASP.NET Web API 与 ninject 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10849132/

相关文章:

javascript - asp.net mvc 显示 ViewBag 字典到 javascript

asp.net-web-api - Asp.net Web Api 的基本项目模板?

c# - C#中线程的合理使用?

unit-testing - 模拟 DBSet,EF 模型优先

javascript - ASP.NET MVC4,Javascript 中的 Razor : Page runs but Syntax Error in VS2012

angularjs - Videogular 显示视频的开始时间和结束时间不正确

c# - 多部分内存流提供程序 : filename?

c# - 使用 YIELD 是返回集合的只读方式吗?

类的 C# 文档 <example><code>...</code></example>

c# - 如何在 C# 中获得最新的整小时减去一小时?