Skip to content

Instantly share code, notes, and snippets.

@moodmosaic
Created March 15, 2012 14:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moodmosaic/2044349 to your computer and use it in GitHub Desktop.
Save moodmosaic/2044349 to your computer and use it in GitHub Desktop.
Post: Using the Web API Dependency Resolver with Castle Windsor
foreach (Type controller in typeof(OrderController).Assembly.GetTypes().Where(type => typeof(IHttpController).IsAssignableFrom(type)))
{
// https://github.com/srkirkland/Inflector/blob/5fd4c818dd172bbe276fb210f25c2e2ceaff019e/Inflector/Inflector.cs
string name = Inflector.Pluralize(controller.Name.Replace("Controller", ""));
container.Register(Component
.For(controller)
.Named(name)
.LifestylePerWebRequest());
}
this.container = new WindsorContainer()
.Install(new WebApiInstaller());
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
serviceType => container.Resolve(serviceType),
serviceType => container.ResolveAll(serviceType).Cast<object>());
this.container = new WindsorContainer()
.Install(new WebMvcInstaller())
.Install(new WebApiInstaller());
GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
serviceType => container.Resolve(serviceType),
serviceType => container.ResolveAll(serviceType).Cast<object>());
ControllerBuilder.Current.SetControllerFactory(
new WindsorControllerFactory(this.container));
using System;
using System.Diagnostics;
using System.Web.Http.Common;
internal class NullLogger : ILogger
{
public void Log(string category, TraceLevel level, Func<string> messageCallback)
{
}
public void LogException(string category, TraceLevel level, Exception exception)
{
}
}
using System;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Http.Common;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Http.Metadata;
using System.Web.Http.Metadata.Providers;
using System.Web.Http.ModelBinding;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;
internal class WebApiInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IHttpControllerFactory>()
.ImplementedBy<WindsorHttpControllerFactory>()
.LifestyleSingleton(),
Component.For<ILogger>()
.ImplementedBy<NullLogger>()
.LifestyleSingleton(),
Component.For<IFormatterSelector>()
.ImplementedBy<FormatterSelector>()
.LifestyleSingleton(),
Component.For<IHttpControllerActivator>()
.ImplementedBy<DefaultHttpControllerActivator>()
.LifestyleTransient(),
Component.For<IHttpActionSelector>()
.ImplementedBy<ApiControllerActionSelector>()
.LifestyleTransient(),
Component.For<IActionValueBinder>()
.ImplementedBy<DefaultActionValueBinder>()
.LifestyleTransient(),
Component.For<IHttpActionInvoker>()
.ImplementedBy<ApiControllerActionInvoker>()
.LifestyleTransient(),
Component.For<ModelMetadataProvider>()
.ImplementedBy<CachedDataAnnotationsModelMetadataProvider>()
.LifestyleTransient(),
Component.For<HttpConfiguration>()
.Instance(GlobalConfiguration.Configuration));
}
}
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using Castle.MicroKernel;
internal class WindsorHttpControllerFactory : IHttpControllerFactory
{
private readonly HttpConfiguration configuration;
private readonly IKernel kernel;
public WindsorHttpControllerFactory(HttpConfiguration configuration, IKernel kernel)
{
this.configuration = configuration;
this.kernel = kernel;
}
public IHttpController CreateController(HttpControllerContext controllerContext, string controllerName)
{
var controller = this.kernel.Resolve<IHttpController>(controllerName);
controllerContext.Controller = controller;
controllerContext.ControllerDescriptor = new HttpControllerDescriptor(this.configuration, controllerName, controller.GetType());
return controllerContext.Controller;
}
public void ReleaseController(IHttpController controller)
{
this.kernel.ReleaseComponent(controller);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment