Nancy provides her own IoC container – TinyIoC, but sometimes you need to use something more powerfull – like e.g. StuctureMap.
Integration of Nancy and SturctureMap is quite easy (as everything in Nancy :). We start with NuGet package installation:
Install-Package Nancy.Bootstrappers.StrucutreMap
And the, we need to create custom bootstrapper (or modify existing if there is one already in your project), which inherits from StructureMapNancyBootstrapper:
public class Bootstrapper : StructureMapNancyBootstrapper { protected override void ConfigureApplicationContainer(IContainer container) { container.Configure(x => { x.Scan(y => y.Assembly(Assembly.GetCallingAssembly())); x.For<IService>().Use<MyService>(); }); } }
In our custom bootstrapper we overload ConfigureApplicationContainer method, in which we can access to container instance used by Nancy. This is a perfect place to configure container and register dependencies.
Leave a Comment