简体   繁体   中英

Ninject.MVC5 not generating 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?

It looks like the most recent Ninject.Web.Common.WebHost 3.3.0 NuGet package no longer includes the NinjectWebCommon.cs. Older versions, such as 3.2.0 do include this file.

Ninject.Web.Common.WebHost 3.3.0 provides a NinjectHttpApplication class you can derive from and use instead of the NinjectWebCommon.cs. The wiki documentation for Ninject does not seem to have been updated but it looks like using the NinjectHttpApplication is one documented approach

see mat's comment - Web API2 NinjectWebCommon.cs do not appear

Install Ninject.MVC5 from Nuget package and keep version 3.2.1 In latest 3.3.0 version it was not adding NinjectWebCommon.cs file so I downgraded version and it has worked for me.

Happy Coding!

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()
        });
    }
}

Install all the below packages it will work automatically.

package id=Ninject version=3.3.4 targetFramework=net451 package id=Ninject.Extensions.Conventions version=3.3.0 targetFramework=net451 package id=Ninject.Extensions.Factory version=3.3.2 targetFramework=net451 package id=Ninject.MVC5 version=3.3.0 targetFramework=net451 package id=Ninject.Web.Commonenter code here version=3.3.1 targetFramework=net451 package id=Ninject.Web.Common.W

  1. Go to: "Manage nuget Packages" contect menu of the project.
  2. Select installed Pakages(You must have installed ninject.mvc5 by then)
  3. Update all associated ninject packages already installed. this could include:
    ninject.web.common, ninject, ninject.web.common.webhost

and you are home with NinjectWebCommon.cs in your App_Start folder

This trick worked for me:

  1. First downgrade all Ninject related package dependencies to version 3.2.0.
  2. Now you will be having the NinjectWebCommon.cs file in App_Start.
  3. Edit the file as per your requirement.
  4. Visit Package Manager UI, it will automatically start to ask you to upgrade all Ninject dependencies to latest one, do it!

Your NinjectWebCommon.cs file will remain intact! Happy coding!

I was facing the same issue,for fixing this i changed the Ninject packages version to 3.2.2.(downgrade)By doing this NinjectWebCommon.cs will be generated.But on running the project it will give error,so switch again to earlier version of Ninject packages. And run the project.The project will run properly

Install-Package Ninject.Web.Common.Webhost This will create the file for you. Happy coding!!!

您需要安装 Ninject.Web.Commom 并创建文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM