title: Registering multiple instances of the same IHostedService
body: <p>I'm trying to register multiple instance of my IHostedService in asp.net core. I need it to subscribe to multiple queues and process messages in them.
  Here's the extension method I implemented to register an instance of my queue subscriber:</p>
  
  <pre><code>        public static IServiceCollection AddQueueSubscription&lt;T&gt;(this IServiceCollection services, string accountName, string queueName,
          Func&lt;IServiceProvider,Func&lt;T, Task&gt;&gt; actionResolver)
      {
          services.AddSingleton&lt;IHostedService&gt;(ctx =&gt;
          {
              return new BackgroundQueueSubscriber(new QueueInfo
                  {
                      Account = accountName,
                      Name = queueName,
                      Action = msg =&gt; actionResolver(ctx)(JsonConvert.DeserializeObject&lt;T&gt;(msg.AsString))
                  },
                  ctx.GetRequiredService&lt;IConfiguration&gt;(),
                  ctx.GetRequiredService&lt;IMetrics&gt;());
          });
          return services;
      }
  </code></pre>
  
  <p>BackgroundQueueSubscriber implements IHostedService.</p>
  
  <p>Despite registering multiple instance I get a runtime error which says "Unable to resolve service for type QueueInfo while attempting to activate BackgroundQueueSubscriber".
  Why is it trying to create an instance disregarding the implementation code I supplied, shown above?</p>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I solved the problem. The problem was, that I used Scrutor nuget to register in DI every class in my assembly as implemented interface, so it picked up BackgroundQueueSubscriber as implementation of IHostedService. As a result it tried to instantiate it, and it caused the exception to be thrown. The solution was to add a filter to Scrutor not to automatically register this class.</p>