将 Ninject 与 Asp.NET Web API Beta ApiController 结合使用

发布于 2025-01-08 17:12:53 字数 1742 浏览 1 评论 0原文

我被困住了。我正在使用此处概述的 wcf web api p6 Ninject 与 WCF 一起工作的方法Web API Preview 5,但是与测试版中的 mvc 实现有很大不同。这里有一篇好文章 http: //www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver 讨论构建您自己的自定义依赖关系解析器,但是我想使用相同的实现我正在使用我的 mvc 视图控制器...例如 Ninject。我也根据文章中的 IoC Unity 示例尝试了一些方法,但还没有任何结果。任何帮助我指明正确方向的帮助将不胜感激。我也会继续自己挖掘。提前致谢!

这就是我现在所在的地方。我使用 WebActivator 来引导代码,但后来我把它放到了 Application_Start() 中,只是为了从等式中多取一件事。

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

我收到以下错误:
Ninject.Web.Mvc.NinjectDependencyResolver 类型似乎未实现 Microsoft.Practices.ServiceLocation.IServiceLocator。
参数名称:commonServiceLocator

找到解决方案
也许有/将会有一种更优雅的方式,但这现在对我有用。我还在这里添加了我的自定义消息处理程序。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雪落纷纷 2025-01-15 17:12:53

我从未使用过 WebAPI,但由于 IDependencyResolver 的语义与 MVC3 中的语义完全相同,因此您应该能够使用相同的实现:https://github.com/ninject/ninject.web.mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs

更新:
Ninject.Web.WebAPi 扩展添加了对 ApiController 的支持

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

沧桑㈠ 2025-01-15 17:12:53

我找到了答案并用解决方案更新了我上面的问题。解决方案本身或多或少存在于 使用 Web API 依赖关系解析器一文,我只需要不断调整 ninject。这两个答案都帮助我快速缩小了范围,所以感谢@Remo 和@James。

I found the answer and updated my question above with the solution. The solution itself was more or less present in the Using the Web API Dependency Resolver article, i just had to keep tweaking for ninject. Both answers helped me quickly narrow this down so thanks to @Remo and @James.

请恋爱 2025-01-15 17:12:53

可能听起来很愚蠢,但您是否确保拥有对 CommonServiceLocator / CommonServiceLocator.NinjectAdapter nuget 包或关联程序集的引用。您的 NinjectDependencyResolver 可能无法解析对 IServiceLocator 的引用。

May sound silly but have you made sure you have a reference to the CommonServiceLocator / CommonServiceLocator.NinjectAdapter nuget package or associated assemblies. Your NinjectDependencyResolver may not be able to resolve the reference to IServiceLocator.

俏︾媚 2025-01-15 17:12:53

相同的代码适用于 MVC 和 WebApi,但是由于 WebApi 受到 MVC 的启发,并且不必引用 MVC 程序集即可使用 WebApi(反之亦然),因此存在大量重复代码两个框架之间。如果您想使用 MVC,您的依赖项将来自 System.Web.Mvc,如果您想使用 WebApi,您将使用 System.Web.Http。如果您想同时使用两者,则必须使用更明确的名称空间解析在代码中区分何时使用哪一个。

在您的情况下,您的问题是 MVC 首先出现,并且 NinjectDependancyResolver 类继承自 System.Web.Mvc.IDependencyResolver。您需要创建 NinjectDependancyResolver 类的精确副本,并改为从 System.Web.Http.IDependencyResolver 继承。使用这个类来设置你的 IoC,你就可以开始了。

The same code will work for both MVC and the WebApi, however because the WebApi was inspired by MVC and the MVC assemblies do not have to be referenced in order to use WebApi (or vise versa), there is a fair amount of duplication of code between the two frameworks. If you want to use MVC you're dependencies will come from System.Web.Mvc, if you want to use WebApi you'll use System.Web.Http. If you want to use both you'll have to differentiate in your code which to use when, using a more explicit namespace resolution.

In your case your problem is that MVC came first and the NinjectDependancyResolver class inherits from System.Web.Mvc.IDependencyResolver. You'll want to create an exact duplicate of the NinjectDependancyResolver class and instead inherit from System.Web.Http.IDependencyResolver instead. Use THIS class to setup your IoC and you'll be good to go.

杯别 2025-01-15 17:12:53

我在这里找到了一个很好的解决方案。

如果您已经在使用 Ninject CommonServiceLocator / Bootstrapper,那么这非常简单:

private static IKernel CreateKernel() {
   var kernel = new StandardKernel();
   RegisterServices(kernel);

   GlobalConfiguration.Configuration.ServiceResolver
      .SetResolver(new NinjectServiceLocator(kernel));
   return kernel; 
}

I found a nice solution here.

It's pretty easy if you're already using the Ninject CommonServiceLocator / Bootstrapper:

private static IKernel CreateKernel() {
   var kernel = new StandardKernel();
   RegisterServices(kernel);

   GlobalConfiguration.Configuration.ServiceResolver
      .SetResolver(new NinjectServiceLocator(kernel));
   return kernel; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文