MonoRail and Windsor integration

May 18, 2008 at 9:00 PMAndre Loker

Although the procedure of enabling Windsor Container integration is explained quite well in the documentation, I ran into some trouble today. As required by the Windsor integration I made my HttpApplication instance implement IContainerAccessor, created a WindsorContainer instance in Application_Start and returned that instance in the Container property. Still the MonoRail runtime was complaining:

The container seems to be unavailable in your HttpApplication subclass

What went wrong? The problem was that the IWindsorContainer field in the HttpApplication was not static. As described in my previous post (which I only wrote just because of that problem) multiple instances of HttpApplication can be created by the ASP.NET runtime to serve multiple requests at once. As a consequence only the first instance of HttpApplication that was created had a valid container assigned to its field. The first subsequent request found only a null value and raised the exception mentioned above.

Register IContainerAccessor in Windsor

If you want to register your HttpApplication as a IContainerAccessor in Windsor I'd recommend something along this line:

   1: public class ContainerAccessor : IContainerAccessor {
   2:     public IWindsorContainer Container {
   3:         get {
   4:             // lookup container accessor 
   5:             return WindsorContainerAccessorUtil.ObtainContainer();
   6:         }
   7:     }
   8:  
   9:     public static void RegisterAtContainer(IWindsorContainer container){
  10:         container.AddComponentLifeStyle<IContainerAccessor, ContainerAccessor>(LifestyleType.Singleton);
  11:     }
  12: }

Of course you can always just register the container on itself to have it injected into components.

Posted in: Castle

Tags: , ,

Comments (1) -

Looking forward for more such stuff. Loved your style of providing the information and the nature of the content.

Pingbacks and trackbacks (1)+