Skip to content

Instantly share code, notes, and snippets.

@paigecook
Created October 9, 2012 19:35
Show Gist options
  • Save paigecook/3860942 to your computer and use it in GitHub Desktop.
Save paigecook/3860942 to your computer and use it in GitHub Desktop.
Example of a Ninject IHttpControllerActivator for an ASP.NET Web Api project.
using System;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using Ninject;
namespace WebApiNinjectIHttpControllerActivator
{
public class NinjectKernelActivator: IHttpControllerActivator
{
private readonly IKernel _kernel;
public NinjectKernelActivator(IKernel kernel)
{
_kernel = kernel;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = (IHttpController) _kernel.Get(controllerType);
request.RegisterForDispose( new Release(()=> _kernel.Release(controller)));
return controller;
}
}
internal class Release : IDisposable
{
private readonly Action _release;
public Release(Action release)
{
_release = release;
}
public void Dispose()
{
_release();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment