title: What is the proper way to inject internal class with embedded DI mechanism
body: <p>Can't find proper way to inject internal class from separate library. Found <a href="https://stackoverflow.com/questions/4775323/injecting-an-internal-helper-class-while-resolving-a-public-class">solution</a> for similar problem for Autofac, but can't find same one for embedded dependency injection mechanism.</p>
  <p>My project structure looks like this:</p>
  <p>Marketplace (asp.net) project:</p>
  <pre><code>public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddMarketplaceDbContext();
      }
  }
  </code></pre>
  <p>Business (library):</p>
  <pre class="lang-cs prettyprint-override"><code>public static class DI
  {
      public static IServiceCollection AddMarketplaceDbContext(
          this IServiceCollection services)
      {
          services.AddSingleton&lt;DeviceHandler&gt;(x =&gt;
          {
              var context = x.GetRequiredService&lt;MarketplaceContext&gt;();
              return new DeviceHandler(context);
          });
          return services.AddDbContext&lt;MarketplaceContext&gt;(options =&gt;
              options.UseNpgsql(&quot;connection_string&quot;));
      }
  }
  
  public class DeviceHandler
  {
      private readonly MarketplaceContext _context;
  
      internal DeviceHandler(MarketplaceContext marketplaceContext)
      {
          _context = marketplaceContext;
      }
  }
  
  internal class MarketplaceContext : DbContext
  {
      public MarketplaceContext(DbContextOptions&lt;MarketplaceContext&gt; options)
          : base(options)
      {
      }
  }
  
  </code></pre>
  <p>And that works, but if I try to inject <code>DeviceHandler</code> like this</p>
  <pre class="lang-cs prettyprint-override"><code>services.AddSingleton&lt;MarketplaceHandler&gt;();
  </code></pre>
  <p>Then I'm getting</p>
  <blockquote>
  <p>System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Business.Core.DeviceHandler Lifetime: Singleton ImplementationType: Business.Core.DeviceHandler': A suitable constructor for type 'Business.Core.DeviceHandler' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.)'</p>
  <p>1 of 2 Inner Exceptions:</p>
  <p>InvalidOperationException: Error while validating the service descriptor 'ServiceType: Business.Core.DeviceHandler Lifetime: Singleton ImplementationType: Business.Core.DeviceHandler': A suitable constructor for type 'Business.Core.DeviceHandler' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.</p>
  <p>2 of 2 Inner Exceptions:</p>
  <p>InvalidOperationException: A suitable constructor for type 'Business.Core.DeviceHandler' could not be located. Ensure the type is concrete and services are registered for all parameters of a public constructor.</p>
  </blockquote>
  <p>Hope that anybody knows better (or proper) solution.</p>