Using Ninject as ASP.NET Core's Dependency Injector but getting a Ninject.ActivationException

553 views
Skip to first unread message

yiglas

unread,
Apr 21, 2017, 7:54:24 PM4/21/17
to ninject
I am trying to replace ASP.net Core's internal dependency injector with Ninject.  I am using Ninject 3.2.2 and targeting the .NET 4.6.2 framework.  The error comes in when I try to add MVC 6 to the IApplicationBuilder via UseMVC() command.

Here is the complete Message:
Ninject.ActivationException: 'Error activating IOptions{MvcDataAnnotationsLocalizationOptions} using binding from IOptions{TOptions} to OptionsManager{TOptions}

A cyclical dependency was detected between the constructors of two services.

Activation path:
  3) Injection of dependency IOptions{MvcDataAnnotationsLocalizationOptions} into parameter dataAnnotationLocalizationOptions of constructor of type MvcDataAnnotationsMvcOptionsSetup
  2) Injection of dependency IConfigureOptions{MvcOptions} into parameter setups of constructor of type OptionsManager{MvcOptions}
  1) Request for IOptions{MvcOptions}

Suggestions:
  1) Ensure that you have not declared a dependency for IOptions{MvcDataAnnotationsLocalizationOptions} 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.

'

Here is the stack trace:
   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__94`1.MoveNext()
   at Microsoft.Extensions.Options.OptionsCache`1.CreateOptions()
   at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
   at Microsoft.Extensions.Options.OptionsCache`1.get_Value()
   at Microsoft.Extensions.Options.OptionsManager`1.get_Value()
   at Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.<>c.<AddMvcCoreServices>b__5_0(IServiceProvider s)
   at MyWebApi.DependencyInjection.Ninject.ServiceProviderNinjectModule.<>c__DisplayClass2_0.<Load>b__0(IContext context)
   at Ninject.Activation.Providers.CallbackProvider`1.CreateInstance(IContext context)
   at Ninject.Activation.Provider`1.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.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.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__94`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Microsoft.AspNetCore.Mvc.Controllers.DefaultControllerFactory..ctor(IControllerActivator controllerActivator, IEnumerable`1 propertyActivators)
   at DynamicInjector86a63ed427d54ddda5fdd70e91b99c55(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.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__94`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.AspNetCore.Mvc.Internal.ActionInvokerFactory..ctor(IEnumerable`1 actionInvokerProviders)
   at DynamicInjector6238571f2b0643efac2a17c7c485496c(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.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.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.ResolutionExtensions.TryGet[T](IEnumerable`1 iterator)


Please help.  If attaching my code helps, just let me know.

yiglas

unread,
Apr 24, 2017, 4:05:28 PM4/24/17
to ninject
Figured out a solution.

1. I removed the services.AddMvc in the ConfigureServices function.
2. Created a class:

    public class MvcJsonMvcOptionsSetup : IOptions<MvcJsonOptions>
   
{
       
public MvcJsonOptions Value => new MvcJsonOptions();
   
}

3. Add the following lines of code to the ConfigureServices function.

services.AddTransient<IOptions<MvcJsonOptions>, MvcJsonMvcOptionsSetup>();

var builder = services.AddMvcCore();
builder
.AddAuthorization();
builder
.AddJsonFormatters();

And it worked.

The key to the "A cyclical dependency was detected between the constructors of two services." was to create my own class with the IOptions<MvcJsonOptions> interface. Then added the Transient above the Microsoft's code that adds it.
Reply all
Reply to author
Forward
0 new messages