Ninject + Web ApI 2: Error activating IFilterProvider using binding from IFilterProvider to DefaultFilterProvider
Asked Answered
A

2

7

I originally had used some custom code I found to handle the dependency resolution in Web API and it worked fine. The issue I ran into was that I wanted to bind a filter to my controller scopes just like you can with MVC. For example, Kernel.BindFilter. The new Ninject.We.WebApi package has the binding extension I want but as soon as I add it to my project I get the following error. I'm not changing any of my code, just referencing a DLL.

Error activating IFilterProvider using binding from IFilterProvider to DefaultFilterProvider A cyclical dependency was detected between the constructors of two services.

Activation path: 3) Injection of dependency IFilterProvider into parameter defaultFilterProviders of constructor of type DefaultFilterProviders 2) Injection of dependency DefaultFilterProviders into parameter filterProviders of constructor of type DefaultFilterProvider 1) Request for IFilterProvider

Suggestions: 1) Ensure that you have not declared a dependency for IFilterProvider on any implementations of the service. 2) Consider combining the services into a single one to remove the cycle. 3) Use property injection instead of constructor injection, and implement IInitializable if you need initialization logic to be run after property values have been injected.

at Ninject.Activation.Context.Resolve() at Ninject.KernelBase.<>c__DisplayClass15.b__f(IBinding binding) at System.Linq.Enumerable.WhereSelectListIterator2.MoveNext() at System.Linq.Enumerable.<CastIterator>d__b11.MoveNext() at System.Linq.Enumerable.d__142.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable
1 source) at Ninject.Web.WebApi.Filter.DefaultFilterProvider.GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor) at System.Web.Http.Controllers.HttpActionDescriptor.b__0(IFilterProvider fp) at System.Linq.Enumerable.d__142.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.OrderedEnumerable1.d__0.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.d__a01.MoveNext() at System.Web.Http.Controllers.HttpActionDescriptor.<RemoveDuplicates>d__3.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.<ReverseIterator>d__a01.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at System.Web.Http.Controllers.HttpActionDescriptor.InitializeFilterPipeline() at System.Lazy1.CreateValue() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Lazy`1.get_Value() at System.Web.Http.Controllers.HttpActionDescriptor.GetFilterPipeline()
at System.Web.Http.Controllers.HttpActionDescriptor.GetFilterGrouping()
at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()

Adjunction answered 3/6, 2014 at 15:33 Comment(0)
L
5

In case anyone else is looking for a potential solution for cyclical dependency for the IFilterProvider, I had to explicitly bind DefaultFilterProviders like this

k.Bind<DefaultFilterProviders>().ToConstant(new DefaultFilterProviders(GlobalConfiguration.Configuration.Services.GetFilterProviders()));

or this

k.Bind<DefaultFilterProviders>().ToSelf().WithConstructorArgument(GlobalConfiguration.Configuration.Services.GetFilterProviders());

If anyone else knows a better way I would love to hear it.

Leif answered 2/4, 2019 at 21:34 Comment(0)
A
0

I figured it out. I added the Nuget package Ninject.Web.WebApi.WebHost.

Adjunction answered 5/6, 2014 at 16:53 Comment(4)
Could you please explain how you solved the problem?Auction
Sure, I added the "Ninject.Web.WebApi.WebHost" package to my project. This package is not pulled in automatically so you need to manually add it.Adjunction
Thanks, but unfortunately it didn't help meAuction
Me neither. I still having the same problem.Simplicity

© 2022 - 2024 — McMap. All rights reserved.