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.WhereSelectListIterator
2.MoveNext() at System.Linq.Enumerable.<CastIterator>d__b1
1.MoveNext() at System.Linq.Enumerable.d__142.MoveNext() at System.Collections.Generic.List
1..ctor(IEnumerable1 collection)
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__14
at System.Linq.Enumerable.ToList[TSource](IEnumerable2.MoveNext() at System.Linq.Buffer
1..ctor(IEnumerable1 source) at System.Linq.OrderedEnumerable
1.d__0.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable
1 source) at System.Linq.Enumerable.d__a01.MoveNext() at System.Web.Http.Controllers.HttpActionDescriptor.<RemoveDuplicates>d__3.MoveNext() at System.Linq.Buffer
1..ctor(IEnumerable1 source) at System.Linq.Enumerable.<ReverseIterator>d__a0
1.MoveNext() at System.Collections.Generic.List1..ctor(IEnumerable
1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at System.Web.Http.Controllers.HttpActionDescriptor.InitializeFilterPipeline() at System.Lazy
1.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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With