Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()

like image 511
oliwa Avatar asked Jun 03 '14 15:06

oliwa


1 Answers

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.

like image 167
user3416682 Avatar answered Nov 07 '22 06:11

user3416682