使用 ninject 绑定 ASP.NET Web API [英] ASP.NET Web API binding with ninject

查看:26
本文介绍了使用 ninject 绑定 ASP.NET Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have just installed the mvc4 rc update and I am trying to build an api application with little luck.

我正在使用 ninject 但无法加载我的控制器.我一直收到错误

I am using ninject but cant get my controllers to load. I keep getting an error

Type 'Api.Controllers.ConsumerController' 没有默认构造函数

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

我对 mvc 和使用注入很陌生,所以请耐心等待.

I am very new to mvc and using injection so please bear with me.

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

I havent done anything special to the default binding that is created via nuget

 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>();
    }        
}

我的控制器看起来像

   private readonly IConsumerRepository _repository;

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

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

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

What do I need to do to get the api controllers to work with ninject?

对不起,如果这是简单的东西

Sorry if this is simple stuff

我尝试了你的建议 Michael 但是在将 webcommon.cs 更改为这个之后

I tried your suggestion Michael however after changing the the webcommon.cs to this

  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();

被称为

程序集Ninject.Web.WebApi.Filter.DefaultFilterProvider"中的方法GetFilters"来自程序集Ninject.Web.WebApi,版本=3.0.0.0,Culture=neutral,PublicKeyToken=c7192dc5380945e7"没有实现.

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 中发生了变化.

I asked Brad Wilson about this and it has changed in MVC4 RC.

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

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

Use this implementation to create a Ninject DependencyResolver for your Web Api: https://gist.github.com/2417226

NinjectWebCommon.cs 中:

// Register Dependencies
RegisterServices(kernel);

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

这篇关于使用 ninject 绑定 ASP.NET Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆