Windsor container and “environments”

February 12th, 2007

I bumped into a challenge today that might be of interest to others. We wanted to have a single web site project, and use it to run tests and to the usual development. During development it should use the standard devel database, for test cases, it should use the Test database which is always dropped/re-created by the test cases.

The problem is that we only have one web.config, and one setting for the connection string. Fortunately the connection string is kept by Windsor, in a properties.config but we couldnt think of a way to signalize the environment to it (test/devel).

Our solution was to create a new entity on Castle Windsor’s: IEnvironmentInfo. This interface allows a programmer to return the environment’s name based on, well, anything he wants. In our case we use IIS and VS.Net’s web server. The latter for standard development, the former to run the test cases. We could then use the web server port to distinguish the environment. If the application is running on 80 (IIS) it is being tested, otherwise it’s just a developer working on it.

Whatever is returned by IEnvironmentInfo is transformed into a define to Windsor’s configuration. So you can have

<?if devel ?>
<?else?> <!-- this is for test -->

In our case we have:

public class WebEnvironment : IEnvironmentInfo
    private readonly HttpApplication app;

    public WebEnvironment(HttpApplication app)
    { = app;

    public string GetEnvironmentName()
        return app.Context.Request.Url.Port == 80 ? "test" : "devel";

And all we have to do is pass that to Windsor when we instantiate it:

container = new WindsorContainer(new XmlInterpreter(), new WebEnvironment(this));

This might make no sense at all if you havent faced the challenge yet, but make sure you re-read this post when it clicks for you ;-)

4 Responses to “Windsor container and “environments””

macournoyer Says:


hammett Says:

Brilliant? I think it’s a big hack ;-)

macournoyer Says:

As MonoRail is a big hack on top of ASP.NET ?

Patrick McEvoy Says:

Like it a lot! Less messing with xmlpoke in nant!

Leave a Reply