Web API2 NinjectWebCommon.cs do not appear
Asked Answered
O

3

16

I am doing an Empty Web API in Visual Studio 2013 Framework 4.5. Obviously NinjectWebCommon.cs do not appear.

I installed via Nuget,

  • Ninject,

  • Ninject.Web.Common,

  • Ninject.MVC5,

  • Ninject.Web.Common.WebHost,

  • Ninject.Web.WebApi,

  • Ninject.web.WebApi.WebHost

    but NinjectWebCommon.cs still does not appear.

What else do I need to install?

Can I add that file manually?

thanks

Orenorenburg answered 29/10, 2017 at 14:8 Comment(0)
R
34

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, as shown below:

public class MvcApplication : NinjectHttpApplication
{
   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
   {
       filters.Add(new HandleErrorAttribute());
   }

   public static void RegisterRoutes(RouteCollection routes)
   {
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

       routes.MapRoute(
           "Default", // Route name
           "{controller}/{action}/{id}", // URL with parameters
           new
           {
               controller = "Home",
               action = "Index",
               id = UrlParameter.Optional
           });
   }

   protected override 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 void RegisterServices(IKernel kernel)
   {
       // e.g. kernel.Load(Assembly.GetExecutingAssembly());
   }

   protected override void OnApplicationStarted()
   {
       base.OnApplicationStarted();

       AreaRegistration.RegisterAllAreas();
       RegisterGlobalFilters(GlobalFilters.Filters);
       RegisterRoutes(RouteTable.Routes);
   }
}
Roundshouldered answered 29/10, 2017 at 16:7 Comment(3)
I couldn't figure out how to resolve a dependency.Tolland
@Alisson you can simply add your dependency into the RegisterServices function. ie kernel.Bind<IMyClass>().To<MyClass>();Improper
For injection in web api controller as well, add this line to CreateKernel method: System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);Cheerless
A
10

Tested with latest Ninject: Create an Empty Web Application and select the checkboxes for Mvc and Web Api

Install Nuget Package: Ninject.Web.WebApi.WebHost

Install Nuget Package: WebActivatorEx

Create a class in App_Start named NinjectWebCommon.cs

 [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
 [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]

 namespace <YOURNAMESPACE>
 {
     public static class NinjectWebCommon
     {
         private static readonly Bootstrapper bootstrapper = new Bootstrapper();

         public static void Start()
         {
             DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
             DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
             bootstrapper.Initialize(CreateKernel);
         }

         public static void Stop()
         {
             bootstrapper.ShutDown();
         }

         private static IKernel CreateKernel()
         {
             var kernel = new StandardKernel();
             kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
             kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

             RegisterServices(kernel);
             return kernel;
         }
         private static void RegisterServices(IKernel kernel)
         {
           //kernel.Bind<IRepo>().ToMethod(ctx => new Repo("Ninject Rocks!"));
         }
     }
 }

Configure your DI in RegisterServices

Add dependency to Mvc or Web Api Controllers constructor parameters

Hope it helps, thanks.

Accomplishment answered 30/11, 2017 at 13:59 Comment(1)
This worked like a charm for me. I'm still unclear as to why they went away from including this automatically. I saw above the way to inherit NinjectHttpApplication in MVC, but from what I can see WebApi behaves a bit differently. I'll investigate more later when I have the time. Thanks again.Figment
T
5

If you want get auto-generated NinjectWebCommon class in App_Start folder, you should change value of Dependency behavior option on Highest in NuGet Package Manager. It'll download all dependencies including NinjectWebCommon. enter image description here

Trichiasis answered 20/8, 2018 at 13:41 Comment(3)
the answer didn't work for me with Ninject.MVC5.3.3.0... VS 2017Gregale
Won't work for me with Ninject.MVC5.3.3.0 VS2019 16.2 But i've just see the file create into the folder but not included into the project. Someone else the same issue?Belgravia
the answer didn't work for me with Ninject.Web.WebAPI.3.3.1... VS 2019 v16.4.2Ilona

© 2022 - 2024 — McMap. All rights reserved.