Giter Club home page Giter Club logo

nancy.bootstrappers.windsor's Introduction

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:

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

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

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

Usage

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.

Customizing

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
}

Contributors

Copyright

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

License

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

nancy.bootstrappers.windsor's People

Contributors

grumpydev avatar thecodejunkie avatar asgerhallas avatar chrisnicola avatar agross avatar ccellar avatar

Watchers

James Cloos avatar Sunil Bhujle avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.