Created
March 15, 2012 14:04
-
-
Save moodmosaic/2044349 to your computer and use it in GitHub Desktop.
Post: Using the Web API Dependency Resolver with Castle Windsor
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
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 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
this.container = new WindsorContainer() | |
.Install(new WebApiInstaller()); | |
GlobalConfiguration.Configuration.ServiceResolver.SetResolver( | |
serviceType => container.Resolve(serviceType), | |
serviceType => container.ResolveAll(serviceType).Cast<object>()); |
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
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)); |
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.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) | |
{ | |
} | |
} |
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.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)); | |
} | |
} |
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.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