Ninject.Web.WebApi icon indicating copy to clipboard operation
Ninject.Web.WebApi copied to clipboard

Ninject causes global WebApi filter to be called twice

Open daniellittledev opened this issue 10 years ago • 9 comments

I have a basic Web.Api only project (no MVC), with a global ActionFilter being called twice. I've narrowed the issue down to the NinjectWebCommon file. It looks like calling bootstrapper.Initialize changes something causing the filter to run twice.

It looks like the issue only effect the OnActionExecuting but not the OnActionExecuted method.

Is this a known issue with Ninject and is there a way I prevent the ActionFilter from being invoked twice?

daniellittledev avatar Apr 06 '14 07:04 daniellittledev

How are you useing Filters? Filter Attributes, Filters configured globally? Filters configured using the Filter extension of Ninject?

remogloor avatar Apr 08 '14 17:04 remogloor

This is regarding a standard Global Action Filter, not using Dependency Injection: config.Filters.Add(new SomeFilterAttribute());

daniellittledev avatar Apr 08 '14 23:04 daniellittledev

I've made a sample repo here to show the issue: https://github.com/Lavinski/WebApi-Ninject

daniellittledev avatar Apr 09 '14 03:04 daniellittledev

Up on this, we've faced with same issue. Config applied via config.Filters.Add() is applied twice.

ig-sinicyn avatar May 23 '17 07:05 ig-sinicyn

Faced the same issue. In my case the filter was applied directly on the method as attribute.

[ValidateModelState]
public async Task<IHttpActionResult> Get() { .. }

After: GlobalConfiguration.Configuration.DependencyResolver = new Ninject.Web.WebApi.NinjectDependencyResolver(kernel);

Ninject.Web.WebApi.Filter.DefaultFilterProvider & Ninject.Web.WebApi.Filter.NinjectFilterProviderare registered as services, which cause the action filter attributes to fire multiple times.

Found few workarounds: 1 - Simply set AllowMultiple to false as explained here. 2 - Unregister the default filters provider as explained here.

There should be a more intelligent way to solve this issue.

yuv4ik avatar Aug 04 '17 07:08 yuv4ik

I have to try to unregister the default filters provider but couldn't able to unregister it.

following statement for unregister throws an exception that tells sequence contain no matching element

var defaultFilterProvider = config.Services.GetFilterProviders().Single(provider => provider is ActionDescriptorFilterProvider);

could you please help me out how to Unregister Ninject.Web.WebApi.Filter.DefaultFilterProvider & Ninject.Web.WebApi.Filter.NinjectFilterProvider filters

nibro7778 avatar Jan 29 '18 02:01 nibro7778

any updates on this.

We are basically trying to add ordering in action filters in webapi. But are blocked by this. Are there any workarounds for achieving this.

deveshv19 avatar Jun 26 '18 05:06 deveshv19

Hello, I faced the same kind of situation, just adding Ninject.Web.WebApi.WebHost package to my project solved the issue (no code change at all). I do not understand why this solves the issue but as this package's goal is to provide "Ninject integration with ASP.NET WebAPI", I guess it changes somehow the way things are managed behind the scene? Just wanted to share in case it helps you moving forward and potentially helps to find a better/cleaner resolution.

fabricebauer avatar Aug 05 '20 15:08 fabricebauer