Skip to content

Instantly share code, notes, and snippets.

@deMD
Created September 8, 2016 07:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deMD/c818f694c678af8ae3476b630ca6cbb0 to your computer and use it in GitHub Desktop.
Save deMD/c818f694c678af8ae3476b630ca6cbb0 to your computer and use it in GitHub Desktop.
Code used to enable Ninject in Umbraco
using Ninject;
using Umbraco.IocTest.BLL.Providers;
using Umbraco.IocTest.Core.Interfaces;
namespace Umbraco.IocTest.BLL
{
/// <summary>
/// The CommonServices are services that are a 1:1 pair with an implemnting class.
/// For example: A single provider interface can only have one implmentation,
/// thus this is registered in the common services.
/// </summary>
public static class CommonServices
{
public static void Register(IKernel kernel)
{
kernel.Bind<ITestProvider>().To<TestProvider>();
}
}
}
using System.Web.Http.Dependencies;
using Ninject;
using Ninject.Web.WebApi;
namespace Umbraco.IocTest.Web.Resolvers
{
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver, System.Web.Mvc.IDependencyResolver
{
private readonly IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(kernel.BeginBlock());
}
}
}
using System;
using System.Web;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.Mvc;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using Umbraco.IocTest.Web;
using Umbraco.IocTest.BLL;
using Umbraco.IocTest.Web.Activators;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]
namespace Umbraco.IocTest.Web
{
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();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
// Calls the BLL common services.
// I have the common bindings on implementation level for easy managing.
CommonServices.Register(kernel);
// Set the resolvers for both the MVC architecture and the Web API architecture in that order
DependencyResolver.SetResolver(new Resolvers.NinjectDependencyResolver(kernel));
GlobalConfiguration.Configuration.DependencyResolver = new Resolvers.NinjectDependencyResolver(kernel);
// Custom Activator. url: https://our.umbraco.org/forum/core/general/55057-714-WebApi-and-Unity-IOC-brakes-BackOffice
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new UmbracoWebApiHttpControllerActivator());
}
}
}
using System;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
namespace Umbraco.IocTest.Web.Activators
{
public class UmbracoWebApiHttpControllerActivator : IHttpControllerActivator
{
private readonly DefaultHttpControllerActivator defaultHttpControllerActivator;
public UmbracoWebApiHttpControllerActivator()
{
this.defaultHttpControllerActivator = new DefaultHttpControllerActivator();
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
IHttpController instance =
this.IsUmbracoController(controllerType)
? Activator.CreateInstance(controllerType) as IHttpController
: this.defaultHttpControllerActivator.Create(request, controllerDescriptor, controllerType);
return instance;
}
private bool IsUmbracoController(Type controllerType)
{
return controllerType.Namespace != null
&& controllerType.Namespace.StartsWith("umbraco", StringComparison.InvariantCultureIgnoreCase);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment