A little nuget for Asp.Net MVC and Windsor

This is my very first code-only package for nuget.

This nuget serves as a mere example on how to integrate Castle Windsor to an ASP.Net MVC application.

First create an Asp.Net MVC project:


The Empty is ok:


Then add a View and a Controller as shown:


Use the “Add Library Package Reference”:


Search for my nuget (the name for now is Jose.MVC.Windsor);


Install it! and you are done. Castle Windsor is integrated to your very first Asp.Net MVC application.

Lets take a deep look to see what is installed.

My nuget has two dependences on other two packages; Castle.Windsor (which also depends on Castle.Core) and WebActivator. So as expected you can see here the three references:


but as I said, my package has only code:


The bootstraper uses WebActivator to hook in to the application events initialize the container, and run all the installers. Also is responsible to set the ControllerFactory:

[assembly: WebActivator.PreApplicationStartMethod (
    typeof(Bootstrapper), "Wire")]

[assembly: WebActivator.ApplicationShutdownMethod (
    typeof(Bootstrapper), "DeWire")]

namespace SuperShop.App_Start
    public static class Bootstrapper
        private static readonly IWindsorContainer container = new WindsorContainer();

        public static void Wire()
            //To be able to inject IEnumerable<T> ICollection<T> IList<T> T[] use this:
            //container.Kernel.Resolver.AddSubResolver(new CollectionResolver(container.Kernel, true));
            //Documentation http://docs.castleproject.org/Windsor.Resolvers.ashx
            //To support typed factories add this:
            //Documentation http://docs.castleproject.org/Windsor.Typed-Factory-Facility.ashx
            var controllerFactory = new WindsorControllerFactory(container.Kernel);

        public static void DeWire()

The controller factory is (I took this code from Castle wiki):

public class WindsorControllerFactory : DefaultControllerFactory
    private readonly IKernel kernel;

    public WindsorControllerFactory(IKernel kernel)
        this.kernel = kernel;

    public override void ReleaseController(IController controller)

    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        if (controllerType == null)
            throw new HttpException(404, 
                string.Format("The controller for path '{0}' could not be found.", 
        return (IController)kernel.Resolve(controllerType);

And the controller installer is:

public class ControllersInstaller : IWindsorInstaller
    public void Install(IWindsorContainer container, IConfigurationStore store)
                            .If(t => t.Name.EndsWith("Controller"))
                            .Configure(c => c.LifeStyle.Transient));

One last thing the package did is to install the handler for the “PerRequest” lifestyle on the web.config (as explained here):



Although the process of creating a nuget package is very well explained in the codeplex site, you mind find useful to look directly at my template, the code is here and it is free for everything.

I hope you find useful this little thing ! :)

blog comments powered by Disqus