Ninject.MVC5没有生成NinjectWebCommon.Cs

我正在Visual Studio 2017版本15.4上开发一个MVC5项目。 我在这里得到了意想不到的结果,这是我以前从未遇到的。 我从nuget安装了Ninject.MVC5软件包。 它安装得很好,没有任何错误或警告。 但问题是它没有在App_Start文件夹中生成NinjectWebCommon.cs文件。 有什么缘故吗?

它看起来像最近的Ninject.Web.Common.WebHost 3.3.0 NuGet包不再包含NinjectWebCommon.cs。 旧版本(例如3.2.0)确实包含此文件。

Ninject.Web.Common.WebHost 3.3.0提供了一个NinjectHttpApplication类,您可以从NinjectWebCommon.cs派生并使用它来代替NinjectWebCommon.cs。 Ninject的wiki文档似乎没有更新,但看起来使用NinjectHttpApplication是一种记录的方法

看到mat的评论 – Web API2 NinjectWebCommon.cs没有出现

经过大量的搜索和测试后,我得到了确切的解决方案,当系统尝试使用上一个答案一次创建多个实例时,我遇到了错误。 这里我需要创建NinjectWebCommon而不inheritance NinjectHttpApplication

 public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); ///  /// Starts the application ///  public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } ///  /// Stops the application. ///  public static void Stop() { bootstrapper.ShutDown(); } ///  /// Creates the kernel that will manage your application. ///  /// The created kernel. ///  /// Creates the kernel that will manage your application. ///  /// The created kernel. private static IKernel CreateKernel() { var kernel = new StandardKernel(); RegisterServices(kernel); return kernel; } ///  /// Load your modules or register your services here! ///  /// The kernel. private static void RegisterServices(IKernel kernel) { kernel.Load(new INinjectModule[] { new Module() }); } } 

但这是参数化构造函数的问题。 为了避免这个问题,我添加了一个方法来创建具体实例 。 所以这是更新的代码..

 public static class NinjectWebCommon { private static readonly Bootstrapper bootstrapper = new Bootstrapper(); ///  /// Starts the application ///  public static void Start() { DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); bootstrapper.Initialize(CreateKernel); } ///  /// Stops the application. ///  public static void Stop() { bootstrapper.ShutDown(); } ///  /// Creates the kernel that will manage your application. ///  /// The created kernel. ///  /// Creates the kernel that will manage your application. ///  /// The created kernel. private static IKernel CreateKernel() { return Container; } public static T GetConcreteInstance() { object instance = Container.TryGet(); if (instance != null) return (T)instance; throw new InvalidOperationException(string.Format("Unable to create an instance of {0}", typeof(T).FullName)); } public static IKernel _container; private static IKernel Container { get { if (_container == null) { _container = new StandardKernel(); _container.Bind>().ToMethod(ctx => () => new Bootstrapper().Kernel); _container.Bind().To(); RegisterServices(_container); } return _container; } } ///  /// Load your modules or register your services here! ///  /// The kernel. private static void RegisterServices(IKernel kernel) { kernel.Load(new INinjectModule[] { new Module() }); } }