MonoRail and IoC container integration

January 20th, 2008

In the past, to use MonoRail with Windsor, all you had to do was

- Use the IContainerAccessor interface on your HttpApplication
- Install the RailsFacility on Windsor
- Use the attribute useWindsorIntegration=true on the MonoRail configuration

And what this did? It set up different Controller, Filter and ViewComponent factories on MonoRail. If you wanted to use Spring.Net or other container, you could set up these factories using the <services> node to override the existing services.

With the latest version, all of these has gone. Well some.

Now what you have to do is:
- Continue to use the IContainerAccessor interface on your HttpApplication
- Install the MonoRailFacility (new name for RailsFacility)

And you’re done.

MonoRail now always uses the ServiceProviderLocator class to obtain a IServiceProvider and uses it as it’s parent container. That’s means that you can use any IoC container by simply making your HttpApplication class implement IServiceProvider (or better, IServiceProviderEx).

public class Global : HttpApplication, IServiceProviderEx
  public object GetService(Type service)
     return ...

By using your implementation of IServiceProvider as parent container, MonoRail will always query your container for services it needs. That allows you to easy override MonoRail services.

In case you are using Windsor, for instance:

public class Global : HttpApplication, IContainerAccessor
  private static WindsorContainer container;

  public void Application_Start(object sender, EventArgs args)
    container = new WindsorContainer();
    container.AddFacility(new MonoRailFacility());


  public IWindsorContainer Container
    get { return container; }

By registering your custom email service, MonoRail will use yours instead of its default one.

Not sure if I made myself clear on this one, if not please let me know :-)

4 Responses to “MonoRail and IoC container integration”

Bill Pierce Says:

I would say it’s not painfully clear. Do I use IServiceProviderEx or IContainerAccessor? Is it ContainerAccessor for Windsor and ServiceProvider for everything else? Should the convention not be to always use ServiceProvider?

I’ve also been out of the Windsor loop for a while so I do not know the purpose of:


hammett Says:

Hey Bill,

IContainerAccessor is Windsor specific. It was kept for backward compatibility.

IServiceProviderEx is the way to go for other containers, yes, you got that right.

The new syntax that you referred to was recently added by Craig. It’s a fluent interface to register components to the MicroKernel.

Michael Davis Says:

It looks like the angle brackets didn’t get escaped in the code sample for the container.AddComponentEx() stuff, so the type parameters aren’t showing up. It’s much more clear what it does when you can see the type parameters.

Zen and the art of Castle maintenance » Blog Archive » Integrating MonoRail with your favorite IoC container Says:

[...] level of integration, however, is something faily recent (10 months old I guess). Quoting an old post: MonoRail now always uses the ServiceProviderLocator class to obtain a IServiceProvider and uses [...]

Leave a Reply