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

本文关键字:之间 构造函数 检测 依赖 循环 服务 两个 ASP | 更新日期: 2023-09-27 18:06:57

我已经切换到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()

请注意,我使用的NinjectServiceProvider来自官方的asp.net依赖注入库。

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

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

我知道这有点晚了,但是我已经解决了这个问题。我认为这与这个问题有间接关系。从本质上讲,检测到的周期性依赖关系实际上是一个Ninject错误。

所以我知道这个问题在Ninject 4.0.0-beta-0134中得到了解决,尽管我不确定确切的提交,因为CI有有限的历史。Git Repo中也没有标签。在Ninject 3.2.3-unstable-012中无法解决。

更多信息请点击此处。