在一个解决方案中的两个项目中使用Ninject,出现循环依赖异常
Using Ninject in two projects in one solution, a cyclical dependency exception
我在同一解决方案的两个项目中使用 Ninject。在带有视图和 Web.API 项目的 MVC 中。但是当我尝试同时 运行 两个项目时,Web API NinjectWebCommon.cs
出现以下异常 -
Error activating ModelValidatorProvider using binding from
ModelValidatorProvider to NinjectDefaultModelValidatorProvider A
cyclical dependency was detected between the constructors of two
services.
但是如果我运行只有Web API项目,我就不会遇到上面提到的异常。
我在两个项目中都有 NinjectWebCommon.cs
文件。
在网络中 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);
}
和 MVC 项目 -
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>();
}
我在 bootstrapper.Initialize(CreateKernel);
的 Start()
方法中遇到了这个异常。有没有人遇到过同样的问题?
更新:如果重要的话,我还在两个项目中使用以下代码启动。
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Path.to.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Path.to.NinjectWebCommon), "Stop")]
这在其他人身上也发生过,参见 Ninject Issue 131。
Arindamat 提出了以下修复方法:
_kernel
.Bind<DefaultModelValidatorProviders>()
.ToConstant(new DefaultModelValidatorProviders(
config.Services.GetServices(
typeof (ModelValidatorProvider))
.Cast<ModelValidatorProvider>()));
这摆脱了循环依赖。
但是,根本原因 通常是 ninject 的错误安装。 This 答案显示了如何修复它。
我在同一解决方案的两个项目中使用 Ninject。在带有视图和 Web.API 项目的 MVC 中。但是当我尝试同时 运行 两个项目时,Web API NinjectWebCommon.cs
出现以下异常 -
Error activating ModelValidatorProvider using binding from ModelValidatorProvider to NinjectDefaultModelValidatorProvider A cyclical dependency was detected between the constructors of two services.
但是如果我运行只有Web API项目,我就不会遇到上面提到的异常。
我在两个项目中都有 NinjectWebCommon.cs
文件。
在网络中 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);
}
和 MVC 项目 -
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>();
}
我在 bootstrapper.Initialize(CreateKernel);
的 Start()
方法中遇到了这个异常。有没有人遇到过同样的问题?
更新:如果重要的话,我还在两个项目中使用以下代码启动。
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Path.to.NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(Path.to.NinjectWebCommon), "Stop")]
这在其他人身上也发生过,参见 Ninject Issue 131。
Arindamat 提出了以下修复方法:
_kernel
.Bind<DefaultModelValidatorProviders>()
.ToConstant(new DefaultModelValidatorProviders(
config.Services.GetServices(
typeof (ModelValidatorProvider))
.Cast<ModelValidatorProvider>()));
这摆脱了循环依赖。
但是,根本原因 通常是 ninject 的错误安装。 This 答案显示了如何修复它。