title: How do I resolve a dependency in a Class Library from the container configured in Startup?
body: <p>The objective is using IoC and being able to [mock dependencies] for unit testing.</p>
  
  <p>The project: .NET Core Web API with multiple class libraries</p>
  
  <p>I'm using Microsoft.Extensions.DependencyInjection for my IoC and I'd like to keep using it if it supports what I'm trying to accomplish.</p>
  
  <p>The problem: At least one class in my assemblies (class libraries) has a dependency that needs to mocked (e.g. using Moq). I completely understand that I can use constructor injection to inject the interface but that doesn't fit the scenario. </p>
  
  <p>What I'm simply trying to accomplish in my assembly is to resolve the dependency using the container that I initiated in my Startup class of the Web API. </p>
  
  <p>How would I do that? And if that isn't possible, what may be another way to accomplish the same thing, i.e. mocking my printer without using dependency injection?</p>
  
  <p>Below is some sample code which hopefully clarifies this a bit.</p>
  
  <p>Web API's Startup.cs (has a reference to assembly which defines printer)</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddScoped&lt;IPrinter, Printer&gt;();
  }
  </code></pre>
  
  <p>In a different assembly, I'd like to resolve an instance of Printer using the same container.</p>
  
  <pre><code>    public interface IPrinter
      {
          void Print(string text);
      }
  
      public class Printer : IPrinter
      {
          public void Print(string text)
          {
              Console.WriteLine("Printing: " + text);
          }
      }
  
  
      public class Task
      {
          public void PrintSomething(string text)
          {
              //do not 'new up' but resolve it from the container of Startup.cs
              var printer = new Printer(); 
              printer.Print(text);
          }
      }
  </code></pre>