The code below defines an object that holds an instance of the Castle Windsor container and sets it up to handle all requests to resolve ASP.NET MVC Controllers. It also includes code to inject instances of HttpRequest and HttpContextBase. This means that you can define injected objects that contain references to HttpContextBase in their constructors (which can be your MVC controllers) and they will receive populated instances of these objects. It is worth noting that if you reference an instance of HttpContext that has not been resolved / injected by your IoC container then there is a high likely hood that you will end up with two or more separate instances which will cause problems (see this article on Injecting into Action Filter Attributes that can suffer this issue).
Please feel free to use this code and let me know if you run into any issues or have recommendations on how it could be improved.
.UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request)));
.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
This can then just be simple called by adding the following to your global.asax code. Note: This expects a “castle” section to be defined in your web.config to resolve non-controller references handled within the WindsorControllerFactory.