title: How to resolve HostedService in Controller
body: <p>I am trying to add a Background Timer in ASP.NET Core 3.0, which periodically executes a task.
  Google led me to <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.0&amp;tabs=visual-studio" rel="noreferrer">this</a>, where I implemented the 'Timed background tasks'.
  However, I'm stuck in resolving the HostedService in the controller.
  I need a specific instance of TimedHealthCheckService so I can call another public function called 'GetAvailableODataUrl()'.</p>
  
  <hr>
  
  <p>In the <code>startup.cs</code> I use the following code:</p>
  
  <pre><code>services.AddHostedService&lt;TimedHealthCheckService&gt;();
  </code></pre>
  
  <p>The TimedHealthCheckService obviously implements IHostedService:</p>
  
  <pre><code>public class TimedHealthCheckService : IHostedService, IDisposable                  
  </code></pre>
  
  <p>In my controller, I have the following constructor:  </p>
  
  <pre><code>public HealthCheckController(ILogger&lt;HealthCheckController&gt; logger, IHostedService hostedService)
  {
    this.logger = logger;
    this.timedHealthCheckService = hostedService as TimedHealthCheckService;
  }
  </code></pre>
  
  <p>However, when I start my WebAPI, the <code>timedHealthCheckService</code> is always null.
  It seems another IHostedService gets injected into the constructor. By checking hostedService, it is actually an object of type GenericWebHostService.</p>
  
  <p>If I change the controller's constructor to:</p>
  
  <pre><code>public HealthCheckController(ILogger&lt;HealthCheckController&gt; logger, TimedHealthCheckService hostedService)
  </code></pre>
  
  <p>I am getting the following error:</p>
  
  <p><em>Unable to resolve service for type 'HealthCheck.Web.TimedHealthCheckService' while attempting to activate 'HealthCheck.Web.Controllers.HealthCheckController'.</em></p>
  
  <p>I also tried <code>services.AddSingleton&lt;IHostedService, TimedHealthCheckService&gt;();</code> with the same result.</p>