title: Injecting Simple Injector components into IHostedService with ASP.NET Core 2.0
body: <p>In ASP.NET Core 2.0, there is a way to add background tasks by implementing the <code>IHostedService</code> interface (see <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosted-services?view=aspnetcore-2.0" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/hosted-services?view=aspnetcore-2.0</a>). By following this tutorial, the way I was able to get it working was by registering it in the ASP.NET Core container. My goal is to be reading messages from a queue and processing the jobs in the background; a message is posted to the queue (via controller action) and then processed in the background on a timed interval.</p>
  
  <pre><code>// Not registered in SimpleInjector
  services.AddSingleton&lt;IHostedService, MyTimedService&gt;(); 
  </code></pre>
  
  <p>When I put this registration in the ASP.NET Core container, it kicks off the process automatically on application startup. However, when I register this in SimpleInjector, the service is not automatically started. I believe this is the case because we only register the SimpleInjector container with the MvcControllers and MvcViewComponents:</p>
  
  <pre><code>// Wire up simple injector to the MVC components
  container.RegisterMvcControllers(app);
  container.RegisterMvcViewComponents(app);
  </code></pre>
  
  <p>The problem I run into is when I want to start injecting components register from SimpleInjector (e.g. Repositories, generic handlers with decorators...) into an implementation of IHostedService as demonstrated below:</p>
  
  <pre><code>public class TimedService : IHostedService, IDisposable
  {
      private IJobRepository _repo;
      private Timer _timer;
  
      public TimedService(IJobRepository repo)
      {
          this._repo = repo;
      }
      ...
      ...
      ...
  }
  </code></pre>
  
  <p>Since <code>IHostedService</code> is registered with ASP.NET Core and not Simple Injector, I receive the following error when running the timed background service:</p>
  
  <blockquote>
    <p>Unhandled Exception: System.InvalidOperationException: Unable to resolve service for type 'Optimization.Core.Interfaces.IJobRepository' while attempting to activate 'Optimization.API.BackgroundServices.TimedService'.    </p>
  </blockquote>
  
  <p>So my question is, what is the best way to implement background tasks in Simple Injector? Does this require a separate integration package than the standard MVC integration? How am I able to inject my Simple Injector registrations into the <code>IHostedService</code>? If we could automatically start the service after being registered in Simple Injector, I think that would solve this problem.</p>
  
  <p>Thank you for any pointers here and for any advice on this topic! I could be doing something wrong. I have really enjoyed using Simple Injector for the past year.</p>