Nancy boostrapper for the Windsor container
C# Other
Latest commit 8680542 Jun 6, 2017 @jchannon jchannon Merge pull request #53 from thecodejunkie/add-github-folder
Added .github folder

Windsor Bootstrapper for Nancy

This is a bootstrapper for using Windsor with Nancy. A couple of things are worth noting about this bootstrapper that differ from the others. First, child containers are a bit of a faux paux with Windsor (if you are really interested you can read a bit about it here, here and here) personally I don't fully understand why, but I've found it is virtually impossible to avoid a memory leak if you are creating one for each request.

Instead we use Windsor 3.0's new Scoped Lifestyles. This scopes the resolution of dependencies that use the 'Scoped' lifestyle to the current call scope. Specifically we wrap the call to INancyEngine.HandleRequest using a dynamic proxy. When the call finishes the scope is disposed of.

Because the CallContext is not reliable when hosting using ASP.NET and IIS we fall back to the standard PerWebRequest scope if we detect HttpContext.Current.

To use this when registering your own dependencies simply use the NancyScopeAccessor class provided like this:

protected override void ConfigureApplicationContainer(IWindsorContainer existingContainer)
  // This dependency uses the default singleton lifestyle
  existingContainer.Register(Component.For<IApplicationDependency, ApplicationDependencyClass>());

  // This dependency is registered per-web-request
  existingContainer.Register(Component.For<IRequestDependency, RequestDependencyClass>().LifestyleScoped<NancyPerWebRequestScopeAccessor>());

See this page with more details on Windsor's new scoped lifstyle and other new Windsor 3.0 features.

Note that if you are hosting under ASP.NET then you will need to register the Castle PerWebRequestModule. There are two ways to do this:

  1. The new 3.0 way: Simply add Microsoft.Web.Infrastructure to your project and it will work automagically (see the WebDemo project for an example)

  2. The pre-3.0 way: Add this to your web.config You will need to add the following module to your web config however:

  <add name="PerWebRequest" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule" />

<modules runAllManagedModulesForAllRequests="true">
  <add name="PerWebRequest" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule" />

I highly recommend the new way, as web.config cruft is unsightly.


When Nancy detects that the WindsorNancyBootstrapper type is available in the AppDomain of your application, it will assume you want to use it, rather than the default one.

The easiest way to get the latest version of WindsorNancyBootstrapper into your application is to install the Nancy.Boostrappers.Windsor nuget.


By inheriting from WindsorNancyBootstrapper you will gain access to the IWindsorContainer of the application and request containers and can perform what ever reqistations that your application requires.

public class Bootstrapper : WindsorNancyBootstrapper
    protected override void ApplicationStartup(IWindsorContainer container, IPipelines pipelines)
        // No registrations should be performed in here, however you may
        // resolve things that are needed during application startup.

    protected override void ConfigureApplicationContainer(IWindsorContainer existingContainer)
        // Perform registrations here

    protected override void RequestStartup(IWindsorContainer container, IPipelines pipelines, NancyContext context)
        // No registrations should be performed in here, however you may
        // resolve things that are needed during request startup.

You can also override the GetApplicationContainer method and return a pre-existing container instance, instead of having Nancy create one for you. This is useful if Nancy is co-existing with another application and you want them to share a single container.

protected override IWindsorContainer GetApplicationContainer()
    // Return application container instance

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community. For more information see the .NET Foundation Code of Conduct.

Contribution License Agreement

Contributing to Nancy requires you to sign a contribution license agreement (CLA) for anything other than a trivial change. By signing the contribution license agreement, the community is free to use your contribution to .NET Foundation projects.

.NET Foundation

This project is supported by the .NET Foundation.


Copyright © 2010 Andreas Håkansson, Steven Robbins and contributors


Nancy.Bootstrappers.Windsor is licensed under MIT. Refer to license.txt for more information.