ASP.NET vNext和Ninject在两个服务的构造函数之间检测到循环依赖

时间:2015-08-08 21:29:29

标签: c# asp.net dependency-injection ninject asp.net-core

我已切换到ASP.NET vNext beta6并开始尝试运行我的应用程序时收到以下异常:

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

请注意,我使用的是来自官方asp.net dependency injection repositoryNinjectServiceProvider

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我知道这已经很晚了,但我解决了这个问题。我认为这与此issue间接相关。实质上,检测到的循环依赖实际上是一个Ninject错误。

所以我知道这个问题已在Ninject 4.0.0-beta-0134中得到解决,但我不确定确切的提交,因为CI限制historyThere also isn't a tag within the Git Repo。它在Ninject 3.2.3-unstable-012中未得到解决。

可以找到更多信息here