使用Ninject有Asp.NET的Web API测试版ApiController [英] Using Ninject with Asp.NET Web API Beta ApiController

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

问题描述

我卡住了。我是用这里概述了WCF的Web API P6方法<一个href=\"http://stackoverflow.com/questions/7602327/ninject-working-with-wcf-web-api-$p$pview-5\">Ninject与WCF的Web API preVIEW 5 ,然而事情在公测MVC实现相当多的不同的工作。有一个很好的文章在这里<一个href=\"http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver\">http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver谈到有关建立自己的自定义依赖解析器,但是我想用我使用的是同一个实现我的MVC视图控制器...例如。 Ninject。我试过基础上,文章中的国际奥委会团结的例子太多了一些东西,但什么也没摇出来呢。任何指着我的方向是正确的帮助将是非常美联社preciated。我要不断挖掘我自己也是如此。在此先感谢!

下面是我在哪里。我用WebActivator来引导code,但因为我已经下降到了的Application_Start()只取一件事的方程。

 保护无效的Application_Start()
    {
        变种内核=新StandardKernel(新MyNinjectModule());
        GlobalConfiguration.Configuration.ServiceResolver.SetResolver(新NinjectDependencyResolver(内核));
    }

和我收到以下错误:结果
类型Ninject.Web.Mvc.NinjectDependencyResolver似乎并没有实现Microsoft.P​​ractices.ServiceLocation.IServiceLocator结果参数名称:commonServiceLocator

找到了解决办法结果
或许有/将是一个更优雅的方式,但这种情况正在为我工​​作。我也在这里加入我的自定义消息处理为好。

  [装配:WebActivator preApplicationStartMethod(typeof运算(MyApp.AppStart.ApiBootstrapper),开始)
命名空间MyApp.AppStart
{
    公共类ApiBootstrapper
    {
        公共静态无效的start()
        {
            变种内核=新StandardKernel(新MyNinjectModule());
            VAR解析器=新NinjectDependencyResolver(内核);
            GlobalConfiguration.Configuration.ServiceResolver.SetResolver(resolver.GetService,resolver.GetServices);
            GlobalConfiguration.Configuration.MessageHandlers.Add(新ApiAuthHandler());
        }
    }
}


解决方案

我从来没有使用过的WebAPI但由于语义的IDependencyResolver的是完全一样从MVC3一个你应该能够使用相同的实现:<一href=\"https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs\" rel=\"nofollow\">https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs

更新:
该Ninject.Web.WebAPi扩展增加了对ApiControllers支持

I'm stuck. I was using the method outlined here for wcf web api p6 Ninject working with WCF Web API Preview 5, however things are quite a bit different with the mvc implementation in the beta. There is a good article here http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver that talks about building your own custom dependency resolver, however i would like to use the same implementation i'm using for my mvc view controllers...e.g. Ninject. I've tried a few things based on the IoC Unity example in the article too, but nothing has panned out yet. Any help pointing me in the right direction would be much appreciated. I'm going to keep digging on my own as well. Thanks in advance!

Here's where I'm at. I was using WebActivator to bootstrap the code but I've since dropped it to the Application_Start() just to take one more thing out of the equation.

    protected void Application_Start()
    {
        var kernel = new StandardKernel(new MyNinjectModule());
        GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
    }

And am receiving the following error:
The type Ninject.Web.Mvc.NinjectDependencyResolver does not appear to implement Microsoft.Practices.ServiceLocation.IServiceLocator.
Parameter name: commonServiceLocator

Found the solution
Perhaps there is/will be a more elegant way but this is now working for me. I'm also adding my custom message handler here as well.

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.AppStart.ApiBootstrapper), "Start")]
namespace MyApp.AppStart
{
    public class ApiBootstrapper
    {
        public static void Start()
        {
            var kernel = new StandardKernel(new MyNinjectModule());
            var resolver = new NinjectDependencyResolver(kernel);
            GlobalConfiguration.Configuration.ServiceResolver.SetResolver(resolver.GetService, resolver.GetServices);
            GlobalConfiguration.Configuration.MessageHandlers.Add(new ApiAuthHandler());
        }
    }
}

解决方案

I never used the WebAPI but since the semantic of the IDependencyResolver is exactly the same as the one from MVC3 you should be able to use the same implementation: https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs

Update: The Ninject.Web.WebAPi extension adds support for ApiControllers

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

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