How to pass IoC container to NancyFX? (OWIN, Unity)

c#, dependency-injection, nancy, owin, unity-container

Solution

Disclaimer: I really don't know if this is the best/cleanest/whatever solution to this problem. But for me it works.

I wrapped my container (Castle Windsor) like this, which is basically a singleton.

public class Container
{
    // static holder for instance, need to use lambda to construct since constructor private
    private static readonly Lazy<IWindsorContainer> instance = new Lazy<IWindsorContainer>(() =>
    {
        var container = new WindsorContainer();
        container.Install(FromAssembly.This());

        return container;
    });

    // private to prevent direct instantiation.
    private Container()
    {
    }

    // accessor for instance
    public static IWindsorContainer Instance
    {
        get
        {
            return instance.Value;
        }
    }
}

Then in my custom bootstrapper I access the already configured container like this

protected override Castle.Windsor.IWindsorContainer GetApplicationContainer()
{
  return Container.Instance;
}

Problem

I have a Windows service where I use OWIN and NancyFX to host a website on top of it. On many places in my service, I use Unity to inject dependencies into classes, mostly services. However, if I use them in any Nancy modules, the dependencies get resolved twice because Nancy uses its own IoC container (TinyIoC). Fortunately, Nancy allows to override the default IoC container generation and use of an existing one by creating a nancy bootstrapper. But how do I pass my existing IUnityContainer to the bootstrapper? Basically, all I have to start OWIN is... ``` WebApp.Start<MyOwinStarter>(url); ``` How can I pass a Unity container to it to pass it further to the nancy bootstrapper?

Original source