Skip to content

Instantly share code, notes, and snippets.

@OutCust
Forked from odytrice/Ninject.Http.cs
Last active August 29, 2015 14:23
Show Gist options
  • Save OutCust/37710aa491b2f7776b51 to your computer and use it in GitHub Desktop.
Save OutCust/37710aa491b2f7776b51 to your computer and use it in GitHub Desktop.

Revisions

  1. @odytrice odytrice renamed this gist Jun 8, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @odytrice odytrice revised this gist May 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Ninject.WebApi.cs
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@
    // NinjectContainer.RegisterAssembly()
    // You are done.

    namespace Ninject.WebApi
    namespace Ninject.Http
    {
    /// <summary>
    /// Resolves Dependencies Using Ninject
  3. @odytrice odytrice revised this gist May 6, 2014. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions Ninject.WebApi.cs
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,7 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Dependencies;
    @@ -17,6 +18,8 @@
    // simply add them to the Modules property of the NinjectModules class
    // 5. Add the following Line to your Global.asax
    // NinjectHttpContainer.RegisterModules(NinjectHttpModules.Modules);
    // 5b.To Automatically Register all NinjectModules in the Current Project, You should instead add
    // NinjectContainer.RegisterAssembly()
    // You are done.

    namespace Ninject.WebApi
    @@ -32,6 +35,12 @@ public NinjectHttpResolver(params NinjectModule[] modules)
    Kernel = new StandardKernel(modules);
    }

    public NinjectHttpResolver(Assembly assembly)
    {
    Kernel = new StandardKernel();
    Kernel.Load(assembly);
    }

    public object GetService(Type serviceType)
    {
    return Kernel.TryGet(serviceType);
    @@ -92,6 +101,13 @@ public static void RegisterModules(NinjectModule[] modules)
    GlobalConfiguration.Configuration.DependencyResolver = _resolver;
    }

    public static void RegisterAssembly()
    {
    _resolver = new NinjectHttpResolver(Assembly.GetExecutingAssembly());
    //This is where the actual hookup to the Web API Pipeline is done.
    GlobalConfiguration.Configuration.DependencyResolver = _resolver;
    }

    //Manually Resolve Dependencies
    public static T Resolve<T>()
    {
  4. @odytrice odytrice revised this gist Jan 20, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion Ninject.WebApi.cs
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,9 @@
    // 1. Install Packages Ninject and Ninject.Web.Common
    // 2. Remove NinjectWebCommon.cs in your App_Start Directory
    // 3. Add this file to your project (preferrably in the App_Start Directory)
    // 4. Add Your Bindings to Concrete Types to Load method of the main Module. You can add as many additional modules as you want, simply add them to the Modules property of the NinjectModules class
    // 4. Add Your Bindings to the Load method of MainModule.
    // You can add as many additional modules to keep things organized
    // simply add them to the Modules property of the NinjectModules class
    // 5. Add the following Line to your Global.asax
    // NinjectHttpContainer.RegisterModules(NinjectHttpModules.Modules);
    // You are done.
  5. @odytrice odytrice revised this gist Oct 23, 2013. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions Ninject.WebApi.cs
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,17 @@
    using System.Web.Http;
    using System.Web.Http.Dependencies;

    // A small Library to configure Ninject (A Dependency Injection Library) with a WebAPI Application.
    // To configure, take the following steps.
    //
    // 1. Install Packages Ninject and Ninject.Web.Common
    // 2. Remove NinjectWebCommon.cs in your App_Start Directory
    // 3. Add this file to your project (preferrably in the App_Start Directory)
    // 4. Add Your Bindings to Concrete Types to Load method of the main Module. You can add as many additional modules as you want, simply add them to the Modules property of the NinjectModules class
    // 5. Add the following Line to your Global.asax
    // NinjectHttpContainer.RegisterModules(NinjectHttpModules.Modules);
    // You are done.

    namespace Ninject.WebApi
    {
    /// <summary>
  6. @odytrice odytrice created this gist Jun 22, 2013.
    88 changes: 88 additions & 0 deletions Ninject.WebApi.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,88 @@
    using Ninject.Modules;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Dependencies;

    namespace Ninject.WebApi
    {
    /// <summary>
    /// Resolves Dependencies Using Ninject
    /// </summary>
    public class NinjectHttpResolver : IDependencyResolver, IDependencyScope
    {
    public IKernel Kernel { get; private set; }
    public NinjectHttpResolver(params NinjectModule[] modules)
    {
    Kernel = new StandardKernel(modules);
    }

    public object GetService(Type serviceType)
    {
    return Kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
    return Kernel.GetAll(serviceType);
    }

    public void Dispose()
    {
    //Do Nothing
    }

    public IDependencyScope BeginScope()
    {
    return this;
    }
    }


    // List and Describe Necessary HttpModules
    // This class is optional if you already Have NinjectMvc
    public class NinjectHttpModules
    {
    //Return Lists of Modules in the Application
    public static NinjectModule[] Modules
    {
    get
    {
    return new[] { new MainModule() };
    }
    }

    //Main Module For Application
    public class MainModule : NinjectModule
    {
    public override void Load()
    {
    //TODO: Bind to Concrete Types Here
    }
    }
    }


    /// <summary>
    /// Its job is to Register Ninject Modules and Resolve Dependencies
    /// </summary>
    public class NinjectHttpContainer
    {
    private static NinjectHttpResolver _resolver;

    //Register Ninject Modules
    public static void RegisterModules(NinjectModule[] modules)
    {
    _resolver = new NinjectHttpResolver(modules);
    GlobalConfiguration.Configuration.DependencyResolver = _resolver;
    }

    //Manually Resolve Dependencies
    public static T Resolve<T>()
    {
    return _resolver.Kernel.Get<T>();
    }
    }
    }