title: Dependency Injection in NuGet package library
body: <p>This builds on concepts discussed in this question
  <a href="https://stackoverflow.com/questions/27060774/how-to-manage-castle-windsor-dependencies-in-a-nuget-library">How to manage Castle Windsor dependencies in a nuget library</a>.</p>
  <p>I am working on a project which uses Castle.Windsor for DI and references several custom NuGet packages. I have installed Castle.Windsor in one of the class libraries (let's call it MyNugetLib) referenced via NuGet and I've defined an extension method on IWindsorContainer in this library which registers the local services.</p>
  <pre><code>public static class WindsorContainerExtensions
  {
      public static void RegisterMyNugetLibServices(this IWindsorContainer container)
      {
          container.RegisterLocalServices();
      }
      
      private static void RegisterLocalServices(this IWindsorContainer container)
      {
          container.Register(...)
      }
  }
  </code></pre>
  <p>The project that references this NuGet package (let's call it MyProj) uses Windsor as well and in the installer it calls the extension method defined in the NuGet package. This all works.
  My question is how do I pass the Windsor container reference to MyNugetLib from MyProj? I have tried injecting IWindsorContainer in the constructor of a class where I need it, but this class uses Lazy instantiation as follows:</p>
  <pre><code>private static readonly Lazy&lt;MyClass&gt; LazyInstance = new Lazy&lt;MyClass&gt;(() =&gt; new MyClass());
      
  private MyClass() {}
  
  public static MyClass Instance =&gt; LazyInstance.Value;
  </code></pre>
  <p>From MyProj this is called as follows:</p>
  <pre><code>Lib.MyClass.Instance
  </code></pre>
  <p>The only way in which I've managed to make this work is exposing a public property in MyClass which is used in MyProj to set the WindsorContainer</p>
  <pre><code>public IWindsorContainer DIContainer { get; set; }
  </code></pre>
  <p>in MyProj</p>
  <pre><code>Lib.MyClass.DIContainer = MyProj.WindsorContainer
  </code></pre>
  <p>I don't particularly like this. Is there a better way of doing this?</p>
  <hr />
  <p>Update: (thanks to insane_developer's suggestions)</p>
  <p>The question really is: how do you inject dependencies in a Lazy constructor? If I could do this, then I could remove the dependency on Windsor from MyNugetLib.</p>
  <p>So I would have</p>
  <pre><code>private IService1 service1;
  private IService2 service2;
  
  private MyClass(IService1 myService1, IService2 myService2) 
  {
      this.service1 = myService1;
      this.service2 = myService2
  }
  
  public static MyClass Instance =&gt; LazyInstance.Value;
  
  private static readonly Lazy&lt;MyClass&gt; LazyInstance = new Lazy&lt;MyClass&gt;(() =&gt; new MyClass(???));
  </code></pre>
  <p>How do I write the func above so it injects dependencies in my constructor?</p>