title: ASP.NET Core IHostedService manual start/stop/pause(?)
body: <p>I would like to implement a recurring (timed) IHostedService instance in ASPNET Core that can be stopped and started on demand.  My understanding is that IHostedService(s) are started by the framework on application startup.  </p>
  
  <p>However, I would like to be able to start/stop the service 'manually', perhaps using an on/off toggle via a UI.  Ideally the "off" state would dispose of currently running service, and the "on" state would then create a new instance. </p>
  
  <p>I've read the MS docs here: <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.1</a>.</p>
  
  <p>My initial thought was to get an instance of the running service and then call the public <code>StopAsync(CancellationToken token)</code> method.  However I'm a little stuck when it comes to which token I should pass in, and the same could be said for the <code>StartAsync(CancellationToken cancellationToken)</code> method.</p>
  
  <p>Any ideas on how this should be done, or if it's even advisable?  Is my approach somehow going against the intended design of hosted services in ASPNET Core?</p>
  
  <p><strong>EDIT 7.27.2018</strong></p>
  
  <p>So it appears after some more research (aka actually <em>reading</em> the documentation :D) that hosted services StartAsync/StopAsync methods are indeed meant to coincide with the  lifetime of the application.  Registered IHostedServices seem to not be added to the DI container for injection into other classes.  </p>
  
  <p>Therefore I do not think my initial idea will work.  For now I registered my services with configuration dependencies (<code>IOptions&lt;T&gt;</code>) that can be updated at runtime.  As the hosted services is processing, it will check the configuration to see if it should continue, otherwise it will just wait (instead of stopping or disposing of the hosted service).</p>
  
  <p>I'll probably mark this as my answer soon, unless I hear of some other ideas.</p>