views:

111

answers:

1

I am looking for an example of how to configure an ASP.NET MVC2 project to use CastleWindsor container to do IoC.

I keep running into problems setting it up, and for every problem there seems to be a solution on-line, but in the end I make so many changes and end up with such a verbose setup to get IoC working using CastleWindsor, that I thought it best to ask this question.

I am looking for the minimum configuration required in the Global.asax page, the Web.config, and if required, what other changes and extension classes are required.

I am not looking to inject into actionfilters at this stage, so just the basics. Preferably not using XML files, but doing it in .NET programatically.

Thank you in advance...

+2  A: 

This is as basic as it gets:

  1. Start a MVC2 project from VS2010
  2. Download MvcContrib for MVC2 (the one that says "extra binaries")
  3. In your project, add a reference to (all these DLLs are included in MvcContrib):

    • Castle.Core.dll
    • Castle.DynamicProxy2.dll
    • Castle.MicroKernel.dll
    • Castle.Windsor.dll
    • MvcContrib.dll
    • MvcContrib.Castle.dll
  4. In your Application_Start(), add these lines (and whatever namespaces are needed):

    var container = new WindsorContainer();
    container.RegisterControllers(typeof(HomeController).Assembly);
    ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));
    
Mauricio Scheffer
I should note that currently MvcContrib includes Windsor 2.0 while the latest version of Windsor is 2.5
Mauricio Scheffer

related questions