1

I'm using Ninject in two projects in the same solution. In MVC with Views and Web.API projects. But I'm having the following exception from Web API NinjectWebCommon.cs when trying to run both project at one time -

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

But if I run only Web API project, I don't encounter the above mentioned exception. I have NinjectWebCommon.cs file in both projects.

In Web API -

public static void Start() //The method looks the same for MVC Project
{
    DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
    DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
    bootstrapper.Initialize(CreateKernel); 
}

private static IKernel CreateKernel()
{
    var kernel = new StandardKernel();
    try
    {
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();           

        GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);    
        RegisterServices(kernel);
        return kernel;
     }
     catch
     {
        kernel.Dispose();
        throw;
     }
}        
private static void RegisterServices(IKernel kernel)
{
    WebIoC.RegisterServices(kernel);
}

And MVC project -

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);
           RegisterServiceLocator(kernel);

           return kernel;
        }
        catch
        {
            kernel.Dispose();
            throw;
        }
}

private static void RegisterServiceLocator(StandardKernel kernel)
{
    var locator = new NinjectServiceLocator(kernel);

    ServiceLocator.SetLocatorProvider(() => locator);
}

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IFormsAuthenticationService>().To<FormsAuthenticationService>();
    kernel.Bind<IAdMembershService>().To<ActiveDirectoryMembershipService>();
    kernel.Bind<IFacebookAuthenticationService>().To<FacebookAuthenticationService>();            
}

I'm encountering this exception in bootstrapper.Initialize(CreateKernel); in Start() method. Has anyone encountered the same issue?

Update: I'm also using the following code for startup in both projects, if it matters.

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Path.to.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Path.to.NinjectWebCommon), "Stop")]
renchan
  • 519
  • 5
  • 24
  • 1
    Try removing this line in your web api and let me know the outcome: GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel); – Darren Gourley Nov 11 '15 at 16:47
  • In this case I'm getting - An exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code Additional information: Sequence contains no elements But from MVC Project NinjectWebCommon.cs now. – renchan Nov 11 '15 at 16:50
  • Are you using the same version of Ninject on both projects (this could be the problem if you are not)? What version are you using? Has this occured after an update? Are the updates done via nuget? – Darren Gourley Nov 11 '15 at 17:02
  • I'm using Ninject 3.2.0.0 for both projects. This exception started appearing when I moved Web API Controllers to a separate Web API project. At first I had both MVC and WebAPI in one project with the one NinjectWebCommon.cs only. I've downloaded the packages via nuget. I think it's also worth mentioning that if I completely exclude NinjectWebCommon.cs from Web API project, MVC project runs fine, without exceptions. – renchan Nov 11 '15 at 17:11
  • What does WebIoC.RegisterServices(kernel); do? – Darren Gourley Nov 11 '15 at 17:48
  • Simply a static method with bindings. – renchan Nov 11 '15 at 18:13
  • By any chance binding the same interfaces to different models? – Darren Gourley Nov 11 '15 at 18:39
  • No, all interfaces are different... – renchan Nov 11 '15 at 18:47
  • Possible duplicate of [NinjectDependencyResolver fails binding ModelValidatorProvider](http://stackoverflow.com/questions/26312231/ninjectdependencyresolver-fails-binding-modelvalidatorprovider) – BatteryBackupUnit Nov 13 '15 at 06:07

1 Answers1

4

This has happened to others before, see Ninject Issue 131.

Arindamat came up with the following fix:

_kernel
    .Bind<DefaultModelValidatorProviders>()
    .ToConstant(new DefaultModelValidatorProviders(
         config.Services.GetServices(
             typeof (ModelValidatorProvider))
         .Cast<ModelValidatorProvider>()));

which gets rid of the cyclic dependency.

However, the root cause is usually a botched up installation of ninject. This answer shows how to fix it.

Community
  • 1
  • 1
BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68