Skip to content
Nancy bootstrapper for the Autofac container
Branch: master
Clone or download
Latest commit bef5a0e Jun 6, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
dependencies
src/Nancy.Bootstrappers.Autofac
test
.editorconfig
.gitignore
.gitmodules
Nancy.Bootstrappers.Autofac.sln
Nancy.Bootstrappers.Autofac.sln.DotSettings
NuGet.config
appveyor.yml
build.cake
build.ps1
build.sh
how_to_build.txt Added proxies for build.ps1/sh Oct 24, 2016
license.txt
readme.md

readme.md

A bootstrapper implementation, for the Nancy framework, based on the Autofac inversion of control container.

Usage

When Nancy detects that the AutofacNancyBootstrapper 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 AutofacNancyBootstrapper into your application is to install the Nancy.Bootstrappers.Autofac nuget.

public interface ISmtpService { }

public class SmtpService : ISmtpService { }

public class Bootstrapper : AutofacNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(ILifetimeScope container)
    {
        container.Update(builder => builder.RegisterType<SmtpService>().As<ISmtpService>().SingleInstance());        
    }
}

Customizing

By inheriting from AutofacNancyBootstrapper you will gain access to the ILifetimeScope of the application and request containers and can perform what ever registrations that your application requires.

public class Bootstrapper : AutofacNancyBootstrapper
{
    protected override void ApplicationStartup(ILifetimeScope 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(ILifetimeScope existingContainer)
    {
        // Perform registration that should have an application lifetime
    }

    protected override void ConfigureRequestContainer(ILifetimeScope container, NancyContext context)
    {
        // Perform registrations that should have a request lifetime
    }

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

Using container builder for registrations is simple.

container.Update(builder => builder.RegisterType<MyImplementation>().As<MyInterface>());

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 ILifetimeScope 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

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

License

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

You can’t perform that action at this time.