Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Created September 8, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/8775ac57c35faaa4df3d04178c7ca7b6 to your computer and use it in GitHub Desktop.
Save anonymous/8775ac57c35faaa4df3d04178c7ca7b6 to your computer and use it in GitHub Desktop.
using System;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using Ninject;
namespace xxxx.Web.Application.Umbraco
{
public class UmbracoWebApiHttpControllerActivator : IHttpControllerActivator
{
private readonly DefaultHttpControllerActivator _defaultHttpControllerActivator;
private readonly IKernel _kernel;
public UmbracoWebApiHttpControllerActivator(IKernel kernel)
{
_kernel = kernel;
this._defaultHttpControllerActivator = new DefaultHttpControllerActivator();
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
if(this.IsUmbracoController(controllerType))
{
return this._defaultHttpControllerActivator.Create(request, controllerDescriptor, controllerType);
}
var controller = (IHttpController)_kernel.Get(controllerType);
request.RegisterForDispose(new Release(() => _kernel.Release(controller)));
return controller;
}
private bool IsUmbracoController(Type controllerType)
{
return controllerType.Namespace != null
&& controllerType.Namespace.StartsWith("umbraco", StringComparison.InvariantCultureIgnoreCase);
}
}
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