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.
public class WindsorControllerFactory : DefaultControllerFactory { IWindsorContainer Container { get; set; } public WindsorControllerFactory(IWindsorContainer container) { Container = container; Container.Register(AllTypes .FromThisAssembly() .BasedOn(typeof(IController)) .Configure(c => c.LifeStyle.Is(LifestyleType.Transient))); Container.Register(Component.For<HttpRequestBase>() .LifeStyle.PerWebRequest .UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request))); Container.Register(Component.For<HttpContextBase>() .LifeStyle.PerWebRequest .UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current))); Container.Register(Component.For<HttpContext() .LifeStyle.PerWebRequest.UsingFactoryMethod(() => HttpContext.Current)); } protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { return (IController)Container.Resolve(controllerType); } public override void ReleaseController(IController controller) { Container.Release(controller); } }
This can then just be simply 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.
protected void Application_Start() { WindsorContainer = new WindsorContainer( new XmlInterpreter(new ConfigResource(“castle”))); ControllerBuilder.Current.SetControllerFactory( new WindsorControllerFactory(WindsorContainer)); …
As part of his fantastic ‘What is .NET standard‘ presentation at DDD12, Adam Ralph provided an amazing amount of detail in such a short amount of time. One of the most valuable points, which is completely obvious when you think about it, is how you should work with .NET standard when creating libraries. NET standard now comes in a multitude of flavours: currently 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6 and 2.0. When starting out . . .
If you’re trying to access a class library (.NET Standard) from a traditional console application (in VS2017 those can be found under ‘Windows Classic Desktop’) you will run into problems; which can feel a little strange for something that was pretty simple in VS2015 and earlier. You can add a reference to the class library project (Resharper will even volunteer to add the dependency / namespace reference if you don’t already have it). But the . . .
Instead of AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
you can call AllTypes.FromThisAssembly()
also you don't need FactorySupportFacility.
Thanks Krzysztof, have updated article and more importantly my code.
Thanks Paul, this is just what I was looking for.
George