Created
October 9, 2012 19:35
-
-
Save paigecook/3860942 to your computer and use it in GitHub Desktop.
Example of a Ninject IHttpControllerActivator for an ASP.NET Web Api project.
This file contains 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
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