的ASP.NET Web API与ninject结合 [英] ASP.NET Web API binding with ninject

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

问题描述

我刚安装了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

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

我的控制器看起来像

My controller looks like

   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

我想你的建议迈克尔然而改变的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>();
    }

我得到一个错误,当

I get an error when

var kernel = new StandardKernel();

被称为

方法'GetFilters'类型'Ninject.Web.WebApi.Filter.DefaultFilterProvider从程序集Ninject.Web.WebApi,版本= 3.0.0.0,文化=中性公钥= 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.

我是什么失踪?

推荐答案

我问布拉德·威尔逊这件事,它有在MVC4 RC改变。

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

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

使用此实现创建Ninject DependencyResolver为您的Web API:
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);

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

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