Ninject.MVC5没有生成NinjectWebCommon.Cs

Jab*_*ria 19 c# asp.net-mvc ninject nuget

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

小智 17

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

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

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

  • 版本3.3.0也没有解析参数化控制器构造函数依赖项. (2认同)

Jab*_*ria 6

经过大量的搜索和测试,我得到了确切的解决方案,当系统尝试使用上一个答案一次创建多个实例时,我遇到了错误.在这里,我只需要创建NinjectWebCommon而不继承 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()
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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()
        });
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 5

从Nuget包中安装Ninject.MVC5并保留版本3.2.1。在最新的3.3.0版本中,它没有添加NinjectWebCommon.cs文件,因此我降级了版本,它对我有用。

编码愉快!