NInject and WebAPI 2?

697 views
Skip to first unread message

Kevin Burton

unread,
Aug 19, 2015, 5:24:14 PM8/19/15
to ninject
I used NuGet to install NInject, and NInject.Web.WebApi. This didn't install NinjectWebCommon.cs. After some Googling, I installed NInject.Web.WebApi.WebHost and that installed the NinjectWebCommon.cs. Then in NInjectWebCommon.cs I changed:
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                // Install our Ninject-based IDependencyResolver into the Web API config
                GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }
and
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IPrincipalOfficeService>().To<PrincipalOfficeService>().InRequestScope();
            kernel.Bind<CRISDBEntities>().ToSelf().InRequestScope();
        }        

My controller is modified as 
        private IPrincipalOfficeService _service;

        public PrincipalOfficeController(IPrincipalOfficeService service)
        {
            this._service = service;
        }

When I run the application I get:


Server Error in '/' Application.

Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider
A cyclical dependency was detected between the constructors of two services.

Activation path:
 3) Injection of dependency ModelValidatorProvider into parameter defaultModelValidatorProviders of constructor of type DefaultModelValidatorProviders
 2) Injection of dependency DefaultModelValidatorProviders into parameter defaultModelValidatorProviders of constructor of type NinjectDefaultModelValidatorProvider
 1) Request for ModelValidatorProvider

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Ninject.ActivationException: Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider
A cyclical dependency was detected between the constructors of two services.

Activation path:
 3) Injection of dependency ModelValidatorProvider into parameter defaultModelValidatorProviders of constructor of type DefaultModelValidatorProviders
 2) Injection of dependency DefaultModelValidatorProviders into parameter defaultModelValidatorProviders of constructor of type NinjectDefaultModelValidatorProvider
 1) Request for ModelValidatorProvider

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


Source Error: 

Line 27:             DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
Line 28:             DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
Line 29:             bootstrapper.Initialize(CreateKernel);
Line 30:         }
Line 31:         

Source File: d:\Source\Corporations\PaperProcessing\Branches\CFOSprint10\Corporations.Rest\App_Start\NinjectWebCommon.cs    Line: 29 

Stack Trace: 

[ActivationException: Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider
A cyclical dependency was detected between the constructors of two services.

Activation path:
  3) Injection of dependency ModelValidatorProvider into parameter defaultModelValidatorProviders of constructor of type DefaultModelValidatorProviders
  2) Injection of dependency DefaultModelValidatorProviders into parameter defaultModelValidatorProviders of constructor of type NinjectDefaultModelValidatorProvider
  1) Request for ModelValidatorProvider

Suggestions:
  1) Ensure that you have not declared a dependency for ModelValidatorProvider 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.
]
   Ninject.Activation.Context.Resolve() +158

Ideas?

Chris Bertrand

unread,
Mar 3, 2017, 2:08:53 PM3/3/17
to ninject
Did you ever find a solution to this? I'm having similar issues atm.

Chris
Reply all
Reply to author
Forward
0 new messages