Web API2 NinjectWebCommon.cs do not appear

拈花ヽ惹草 提交于 2019-11-28 21:29:49

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!