ASP.NET vNext and Ninject A cyclical dependency was detected between the constructors of two services
Asked Answered
D

1

6

I have switched to ASP.NET vNext beta6 and started to receive the following exception trying to run my application:

Ninject.ActivationException: Error activating IOptions{MvcJsonOptions} using binding from IOptions{TOptions} to OptionsManager{TOptions}
A cyclical dependency was detected between the constructors of two services.

Activation path:
  4) Injection of dependency IOptions{MvcJsonOptions} into parameter jsonOptions of constructor of type MvcJsonMvcOptionsSetup
  3) Injection of dependency IConfigureOptions{MvcOptions} into parameter setups of constructor of type OptionsManager{MvcOptions}
  2) Injection of dependency IOptions{MvcOptions} into parameter optionsAccessor of constructor of type ControllerActionDescriptorProvider
  1) Request for IActionDescriptorProvider

Suggestions:
  1) Ensure that you have not declared a dependency for IOptions{MvcJsonOptions} 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.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__1`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at Microsoft.Framework.OptionsModel.OptionsManager`1.Configure(String optionsName)
   at Microsoft.Framework.OptionsModel.OptionsManager`1.GetNamedOptions(String name)
   at Microsoft.Framework.OptionsModel.OptionsManager`1.get_Options()
   at Microsoft.AspNet.Mvc.ApplicationModels.DefaultApplicationModelProvider..ctor(IOptions`1 mvcOptionsAccessor)
   at DynamicInjector17db4d7dd8164be68f856c18b6811a3a(Object[] )
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__1`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider..ctor(IControllerTypeProvider controllerTypeProvider, IEnumerable`1 applicationModelProviders, IOptions`1 optionsAccessor)
   at DynamicInjectorb26d26f3e9ae47cf8e8c7234b167fb59(Object[] )
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
   at Ninject.Activation.Context.ResolveInternal(Object scope)
   at Ninject.Activation.Context.Resolve()
   at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
   at System.Linq.Enumerable.WhereSelectListIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__1`1.MoveNext()
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source)
   at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredServices[T](IServiceProvider provider)
   at Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.GetCollection()
   at Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.get_ActionDescriptors()
   at Microsoft.AspNet.Mvc.Routing.AttributeRoute.GetInnerRoute()
   at Microsoft.AspNet.Mvc.Routing.AttributeRoute.RouteAsync(RouteContext context)
   at Microsoft.AspNet.Routing.RouteCollection.<RouteAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNet.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Microsoft.AspNet.Diagnostics.ErrorHandlerMiddleware.<Invoke>d__5.MoveNext()

Note that I am using NinjectServiceProvider from official asp.net dependency injection repository.

Does anyone know how to fix this issue?

Disassemble answered 8/8, 2015 at 21:29 Comment(6)
@mason I'm not using any of the dependencies mentioned in the error. I believe that the error emerges from one of the bindings in service.AddMvc() which is controlled by asp.net team. Just thought that somebody have already encountered the same issue and found the solution.Disassemble
Care to explain down-vote?Disassemble
I'm encountering this same issue now with beta6; did you find a resolution to this or is this possibly a bug in beta6?Deforest
@JoshuaBarron unfortunately no. Since my application is not so huge - I have managed to switch from Ninject to their default ASP.NET vNext container. However, I needed to jump through some hoops, for instance I was using conventional binding with Ninject.extensions to locate depedancy automatically. With vNext container - I needed to write some bits of reflection logic to do the same thing.Disassemble
I'm in the same boat - I've been wanting to stick with Ninject mostly for the conventions extension, but I guess I will take a look at just wiring up the conventions myself.Deforest
@JoshuaBarron I believe that in future ASP.NET team will fix this issue and provide us with official implementations of providers for popular DI containers because from my perspective their implementation is pretty basic and would not be suitable for most of large projects which currently using other DI containers.Disassemble
C
1

I know this is late, but I solved this problem. I believe this is indirectly related to this issue. In essence, the cyclical dependency detected was really a Ninject bug.

So I know this issue is resolved in Ninject 4.0.0-beta-0134, although I not sure the exact commit as the CI has limited history. There also isn't a tag within the Git Repo. It is not resolved in Ninject 3.2.3-unstable-012.

More information can be found here.

Cartilaginous answered 25/7, 2017 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.