Coder Social home page Coder Social logo

ninject-webapi-2-and-mvc5's Introduction

Ninject Integration in WebApi 2 And Mvc5

How to integrate Ninject in a WebApi 2 And Mvc5 Project
Test Mvc in Index page example url: "http://localhost/WebApiAndMvc5/mvctest/Index"
Test Web Api 2 calling the Get Method "IsServiceUp" example url: "http://localhost/WebApiAndMvc5/IsServiceUp"

  1. Create an Empty Web Application and select the checkboxes for Mvc and Web Api

  2. Install Nuget Package: Ninject.Web.WebApi.WebHost, this package will install: Ninject, Ninject.Web.Common, Ninject.Web.Common.WebHost, Ninject.Web.WebApi, Ninject.Web.WebApi.WebHost but with the latest Release it won't create the "NinjectWebCommon" class in "App_Start" anymore, so you'll have to create it manually as explained further on.

  3. Install Nuget Package: WebActivatorEx

  4. Install Nuget Package: Ninject.MVC5, this package is required for MVC not for Web Api.

  5. Create a class in App_Start named NinjectWebCommon.cs

     using Microsoft.Web.Infrastructure.DynamicModuleHelper;
     using Ninject;
     using Ninject.Web.Common;
     using Ninject.Web.Common.WebHost;
     using System;
     using System.Web;
             
     [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!"));
             }
         }
     }
    
  6. Configure your DI in RegisterServices

  7. Add dependency to Mvc or Web Api Controllers constructor parameters

ninject-webapi-2-and-mvc5's People

Contributors

skydivingangel avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.