Ninject.Web.WebApi
Ninject.Web.WebApi copied to clipboard
Ninject causes global WebApi filter to be called twice
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?
How are you useing Filters? Filter Attributes, Filters configured globally? Filters configured using the Filter extension of Ninject?
This is regarding a standard Global Action Filter, not using Dependency Injection:
config.Filters.Add(new SomeFilterAttribute());
I've made a sample repo here to show the issue: https://github.com/Lavinski/WebApi-Ninject
Up on this, we've faced with same issue. Config applied via config.Filters.Add()
is applied twice.
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.NinjectFilterProvider
are 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.
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
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.
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.