title: How do I get a reference to an IHostedService via Dependency Injection in ASP.NET Core?
body: <h1>Details</h1>
  
  <p>I have attempted to create a background processing structure using the recommended <code>IHostedService</code> interface in ASP.NET 2.1. I register the services as follows:</p>
  
  <pre><code>services.AddSingleton&lt;AbstractProcessQueue&lt;AbstractImportProcess&gt;&gt;();
  services.AddHostedService&lt;AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;&gt;();
  
  services.AddSignalR();
  </code></pre>
  
  <p>The <code>AbstractProcessQueue</code> is just a wrapper around a <code>BlockingCollection</code> of processes that can be enqueued and dequeued. The <code>AbstractBackgroundProcessService</code> implements the <code>IHostedService</code> interface and looks at the queue for new processes it can start.</p>
  
  <p>Now, the trouble starts when, inside a <code>SignalR</code> hub, I attempt to get a reference to the background processing service via the <code>Dependency Injection</code> mechanisms. I have tried the following solutions, but none seem to be working as intended:</p>
  
  <h3>Option 1:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This returns null.
      var service = provider.GetService&lt;AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;&gt;();
  }
  </code></pre>
  
  <h3>Option 2:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This returns null.
      var service = (AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;) provider.GetService(typeof(AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;&gt;));
  }
  </code></pre>
  
  <h3>Option 3:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This throws an exception, because the service is missing.
      var service = provider.GetRequiredService&lt;AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;&gt;();
  }
  </code></pre>
  
  <h3>Option 4:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This throws an exception, because the service is missing.
      var service = (AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;) provider.GetRequiredService(typeof(AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;);
  }
  </code></pre>
  
  <h3>Option 5:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This returns a correct service, but prevents me from adding additional AbstractBackgroundProcessService implementations with different type parameters.
      //Additionally, it seems like this reference was newly created, and not the instance that was created on application startup (i.e. the hash codes are different, and the constructor is called an additional time).
      var service = provider.GetService&lt;IHostedService&gt;();
      if(service is AbstractBackgroundProcessService&lt;AbstractProcessService&gt;)
      {    this.Service = (AbstractBackgroundProcessService&lt;AbstractProcessService&gt;) service;}
  }
  </code></pre>
  
  <h3>Option 6:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This works similarly to the previous option, and allows multiple implementations, but the constructor is still called twice and the instances thus differ.
      AbstractBackgroundProcessService&lt;AbstractImportProcess&gt; service = null;
      foreach(IHostedService service in provider.GetServices&lt;IHostedService&gt;())
      {
          if(service is AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;)
          {
              service = (AbstractBackgroundProcessService&lt;AbstractImportProcess&gt;) service;
              break;
          }
      }  
  }
  </code></pre>
  
  <h3>Option 7:</h3>
  
  <pre><code>public HubImportClient(IServiceProvider provider)
  {
      //This just skips the for each loop all together, because no such services could be found.
      AbstractBackgroundProcessService&lt;AbstractImportProcess&gt; service = null;
      foreach(AbstractBackgroundProcessService&lt;AbstractImportProcess&gt; current in provider.GetServices&lt;AbstractBackgroundProcessService&lt;AbstractImportProcess&gt; &gt;())
      {
          service = current;
          break;
      }    
  }
  </code></pre>
  
  <h3>Option 8:</h3>
  
  <pre><code>//This works, but prevents multiple implementations again.
  public HubImportClient(IHostedService service)
  {
      this.Service = service;   
  }
  </code></pre>
  
  <h3>Option 9:</h3>
  
  <pre><code>//This does not work again.
  public HubImportClient(AbstractBackgroundProcessService&lt;AbstractImportProcess&gt; service)
  {
      this.Service = service;   
  }
  </code></pre>
  
  <h1>Question</h1>
  
  <p>So then my question remains: how am I supposed to get a reference to an <code>IHostedService</code> implementation so that:</p>
  
  <p>(a): I can inject multiple instances of the service that differ only by their type parameter (e.g. a hosted service for <code>AbstractImportProcess</code>es as well as one for <code>AbstractExportProcess</code>es)</p>
  
  <p>(b): there is only ever one instance of the <code>IHostedService</code> for that specific type parameter.</p>
  
  <p>Thanks in advance for any help!</p>