Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ninject causes global WebApi filter to be called twice #10

Open
daniellittledev opened this issue Apr 6, 2014 · 9 comments
Open

Ninject causes global WebApi filter to be called twice #10

daniellittledev opened this issue Apr 6, 2014 · 9 comments

Comments

@daniellittledev
Copy link

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?

@remogloor
Copy link
Owner

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

@daniellittledev
Copy link
Author

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

@daniellittledev
Copy link
Author

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

@ig-sinicyn
Copy link

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

@yuv4ik
Copy link

yuv4ik commented Aug 4, 2017

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.

@nibro7778
Copy link

nibro7778 commented Jan 29, 2018

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

@deveshv19
Copy link

deveshv19 commented Jun 26, 2018

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.

@fabricebauer
Copy link

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.

@vahid42
Copy link

vahid42 commented Apr 18, 2023

I solved my problem just by setting AllowMultiple to false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants