title: Register multiple .Net Core IHostedService in Dependency Injection - Background service in .net core
body: <p>I have a .Net core (v2.2) API. That API uses some external service calls. We want to make them as fire and forget calls. We used .net core Background service to implement that. We have multiple background services. Now if I register all those IHostedService in Dependency Injection, The last registered background service works, and others come as null in constructor injection of project. Registering dependencies like:</p>
  
  <pre><code>services.AddHostedService&lt;BackgroundServiceA&gt;();
   services.AddHostedService&lt;BackgroundServiceB&gt;();
  </code></pre>
  
  <p>And also, I tried adding them as:</p>
  
  <pre><code> services.AddSingleton&lt;IHostedService, BackgroundServiceA&gt;();
   services.AddSingleton&lt;IHostedService, BackgroundServiceB&gt;();
  </code></pre>
  
  <p>In both the cases, only BackgroundServiceB works, BackgroundServiceA comes as null in constructor injection.</p>
  
  <p>To handle this, I was using ServiceProvider to get the object.</p>
  
  <pre><code>  var services = serviceProvider.GetServices&lt;IHostedService&gt;();
   _backgroundServiceA = services.First(o =&gt; o.GetType() == typeof(BackgroundServiceA)) as BackgroundServiceA;
  </code></pre>
  
  <p>Is it a good way to handle such an issue or I am missing something while registering dependencies.</p>
  
  <p>Also, can we trigger both the background calls parallel?</p>