I agreed with Cristiano - this is how I did it, but it was a combination of both:
public class ContainerHostDependencyResolver : IDependencyResolver { private static readonly IServiceLocator Locator = new ServiceLocator(); private readonly IDependencyResolver _defaultResolver; private static readonly Type ControllerFactoryType = typeof (IControllerFactory); public ContainerHostDependencyResolver(IDependencyResolver defaultResolver) { _defaultResolver = defaultResolver; } public object GetService(Type serviceType) { if (ControllerFactoryType.IsAssignableFrom(serviceType)) return Locator.Resolve(serviceType); return _defaultResolver.GetService(serviceType); } public IEnumerable<object> GetServices(Type serviceType) { return _defaultResolver.GetServices(serviceType); } } protected void Application_Start() { DependencyResolver.SetResolver(new ContainerHostDependencyResolver(DependencyResolver.Current)); }
EDIT # 1: This is a safer, simpler approach that does what the reference to the Cristiano tutorial requires. In my case, I have a Windsor wrapped behind our own name ContainerHost / ServiceLocator.
public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { ControllerBuilder.Current.SetControllerFactory(new ContainerHostControllerFactory()); } } public class ContainerHostControllerFactory : DefaultControllerFactory { private static readonly IServiceLocator Locator = new ServiceLocator(); public override IController CreateController(RequestContext requestContext, string controllerName) { return Locator.Resolve<IController>(controllerName + "Controller"); } public override void ReleaseController(IController controller) { Locator.Release(controller); } }
source share