简体   繁体   English

使用Ninject的ASP.NET WebAPI ActionFilters的依赖注入不起作用

[英]Dependency Injection for ASP.NET WebAPI ActionFilters using Ninject not working

I am attempting to set up DI on ActionFilters in ASP.NET WebAPI using Ninject. 我正在尝试使用Ninject在ASP.NET WebAPI中设置ActionFilters上的DI。 I followed the instructions here: https://github.com/ninject/Ninject.Web.WebApi/wiki/Dependency-injection-for-filters 我按照这里的说明操作: https//github.com/ninject/Ninject.Web.WebApi/wiki/Dependency-injection-for-filters

I create my ActionFilter like so: 我这样创建我的ActionFilter:

public class ApiAuthorizeFilter : AbstractActionFilter
{
    private readonly IValidateApiTokenService _validateApiTokenService;

    public ApiAuthorizeFilter(IValidateApiTokenService validateApiTokenService)
    {
        _validateApiTokenService = validateApiTokenService;
    }

    public override bool AllowMultiple => true;

    public override void OnActionExecuting(HttpActionContext actionContext)
    {
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
    }
}

I configured like so: 我这样配置:

kernel.BindHttpFilter<ApiAuthorizeFilter>(FilterScope.Controller);

My understanding based on the info at the above link is that the ActionFilter should then run for all Actions in all WebAPI controllers. 基于上述链接的信息,我的理解是ActionFilter然后应该运行所有WebAPI控制器中的所有Actions。 However, I've set breakpoints at both overridden methods in the filter and it never gets hit. 但是,我在过滤器中的两个重写方法中都设置了断点,它永远不会被击中。 I have set a breakpoint at the configuration and can confirm that it is being executed. 我在配置中设置了一个断点,可以确认它正在执行。

What am I missing? 我错过了什么? I need this ActionFilter to run on all Actions in every ApiController in my project. 我需要这个ActionFilter来运行我项目中每个ApiController中的所有Actions。

in your Startup Class 在您的启动类中

public void Configuration(IAppBuilder app)
{    
    var kernel = new StandardKernel();
    // register IValidateApiTokenService
    var config = new HttpConfiguration();
    config.Filters.Add(new ApiAuthorizeFilter(kernel.Get<IValidateApiTokenService>());
}

The reason it doesn't work per say is because ActionFilters (or any filters for that matter) are created as singletons by the runtime (one instance forever on the app context) and in general any DI container has problems wiring up a object's transient and or disposable dependencies if that object is a singleton. 每个人说不起作用的原因是因为ActionFilters(或任何过滤器)由运行时创建为单例(永远在应用程序上下文中的一个实例),并且通常任何DI容器在连接对象的瞬态和或者一次性依赖项,如果该对象是单例。

One solution to your problem would be to use the built in service locator like so: DependencyResolver.Current.GetService(typeof(IValidateApiTokenService)); 您的问题的一个解决方案是使用内置服务定位器,如下所示: DependencyResolver.Current.GetService(typeof(IValidateApiTokenService));

I don't personally use Ninject for my DI needs but if the above doesn't work it probably needs some wiring up in the DI startup or use a integration package like this one: https://github.com/ninject/ninject.web.mvc/wiki/MVC3 我不亲自使用Ninject来满足我的DI需求,但如果上述功能不起作用,可能需要在DI启动时进行一些连接或使用像这样的集成包: https//github.com/ninject/ninject。 web.mvc /维基/ MVC3

I never worked on Ninject, I worked on Unity. 我从未在Ninject工作,我在Unity工作。 With my experience of working on Unity I will try to shed some light on the issue. 凭借我在Unity上工作的经验,我将尝试阐明这个问题。

I am not sure what is the purpose of _validateApiTokenService , but what I can observer from the above code is that you have to configure to bind the dependency to the implementation IValidateApiTokenService . 我不确定_validateApiTokenService的目的是_validateApiTokenService ,但我从上面的代码中观察到的是你必须配置将依赖项绑定到实现IValidateApiTokenService Along with the following code 以下代码

kernel.BindHttpFilter<ApiAuthorizeFilter>(FilterScope.Controller);

you may have to have some thing like 你可能需要有一些东西

kernel.Bind<IValidateApiTokenService>().To<ValidateApiTokenService>();

or 要么

kernel.Bind<IValidateApiTokenService>.To<ValidateApiTokenService>().InSingletonScope(); 

if it is to be in Singleton scope. 如果它是在Singleton范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Microsoft Asp.net WebApi创建依赖项注入 - How to create a Dependency Injection using Microsoft Asp.net WebApi ASP.NET Web API 中的依赖注入(Ninject) - Dependency Injection in ASP.NET Web API (Ninject) 使用ninject的依赖注入不起作用 - Dependency Injection using ninject not working 具有依赖项注入的ASP.NET WebApi模型绑定 - ASP.NET WebApi Model Binding with Dependency Injection ASP.NET WebAPI中的依赖注入:IHttpControllerActivator与IDependencyResolver - Dependency injection in ASP.NET WebAPI: IHttpControllerActivator vs. IDependencyResolver Ninject和依赖注入WebApi属性 - Ninject and Dependency Injection WebApi Attributes 在ASP.NET中使用Unity进行依赖项注入 - Dependency Injection using Unity in ASP.NET 具有Ninject依赖注入的ASP.NET MVC应用程序中的MongoDB官方C#驱动程序 - MongoDB Official C# Driver in ASP.NET MVC app with Ninject dependency injection 为什么我应该在ASP.Net应用程序中使用IoC Container(Autofac,Ninject,Unity等)进行依赖注入? - Why should I use IoC Container (Autofac, Ninject, Unity etc) for Dependency Injection in ASP.Net Applications? Asp.net MVC样板依赖注入不起作用 - Asp.net MVC boilerplate dependency injection not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM