Created
January 17, 2014 03:13
-
-
Save lnickers2004/8467795 to your computer and use it in GitHub Desktop.
WebAPI Ninject Dependency Reslover-- we need webapicontrib Ninject files from packagemanager for dependency resolution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: WebActivator.PreApplicationStartMethod(typeof(CountingKs.App_Start.NinjectWebCommon), "Start")] | |
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(CountingKs.App_Start.NinjectWebCommon), "Stop")] | |
namespace CountingKs.App_Start | |
{ | |
using System; | |
using System.Web; | |
using System.Web.Http; | |
using CountingKs.Data; | |
using Microsoft.Web.Infrastructure.DynamicModuleHelper; | |
using Ninject; | |
using Ninject.Web.Common; | |
using WebApiContrib.IoC.Ninject; | |
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> | |
private static IKernel CreateKernel() | |
{ | |
var kernel = new StandardKernel(); | |
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel); | |
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); | |
// Support WebAPI | |
//Note we needed to use nuget package manager to install WebApiContrib | |
//https://www.nuget.org/packages/WebApiContrib.IoC.Ninject/0.9.3 | |
//using the following pm console command: | |
// PM> Install-Package WebApiContrib.IoC.Ninject | |
//this allows Ninject to satisfy webapi dependencies | |
GlobalConfiguration.Configuration.DependencyResolver = | |
new NinjectResolver(kernel); | |
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.Bind<ICountingKsRepository>().To<CountingKsRepository>(); | |
kernel.Bind<CountingKsContext>().To<CountingKsContext>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment