Created
September 8, 2016 09:04
-
-
Save anonymous/8775ac57c35faaa4df3d04178c7ca7b6 to your computer and use it in GitHub Desktop.
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 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