问题描述
我正在Visual Studio 2017 15.4版上开发MVC5项目.我在这里得到了前所未有的结果,这是我从未遇到过的.我已经从nuget
安装了Ninject.MVC5
软件包.它安装得很好,没有发出任何错误或警告.但是问题是它没有在App_Start
文件夹中生成NinjectWebCommon.cs
文件.有什么理由吗?
I'm developing a MVC5 project on Visual Studio 2017 Version 15.4. I'm getting unexpected result here what I never faced before. I've installed Ninject.MVC5
package from nuget
. It's installing nicely and not giving any error or warning. But problem is it's not generating NinjectWebCommon.cs
file in App_Start
folder. Is there any reason?
推荐答案
经过大量的搜索和测试,我得到了确切的解决方案,在系统尝试一次与以前的示例一次创建多个实例的同时,我遇到了错误.回答.在这里,我只需要创建NinjectWebCommon
类,而无需继承 NinjectHttpApplication
.
After lot of search and tests, I've got the exact solution, where I faced error while system was trying to create multiple instances at a time with the previous answer. Here I needed to create NinjectWebCommon
class only without inheriting NinjectHttpApplication
.
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
RegisterServices(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Load(new INinjectModule[]
{
new Module()
});
}
}
但是这是参数化构造函数的问题.为避免此问题,我添加了一种创建具体实例的方法.所以这是更新的代码.
But here is a problem with parameterized constructor. To avoid this issue I added a method to create Concrete Instance. So here is the updated code..
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
private static IKernel CreateKernel()
{
return Container;
}
public static T GetConcreteInstance<T>()
{
object instance = Container.TryGet<T>();
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<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
_container.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(_container);
}
return _container;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Load(new INinjectModule[]
{
new Module()
});
}
}
这篇关于Ninject.MVC5不生成NinjectWebCommon.Cs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!