title: How to run a background service on demand - not on application startup or on a timer
body: <p>In a .Net 5 Web API, I would like to run a background task that sends out bulk emails and SMSes.
  I know I can create a service that inherits from BackgroundService, and then add it to the DI Container in the Startup.ConfigureServices method like this:</p>
  <pre><code>services.AddHostedService&lt;EmailAndSmsService&gt;();
  </code></pre>
  <p>But that runs the service immediately - i.e. on application startup.
  I would like to run the service when the API receives a request from the front-end. i.e. in a controller's action method.</p>
  <p>I've been looking at &quot;Background tasks with hosted services&quot; on Microsoft's documentation, and if I'm not mistaken, this is what i need to do (Look at the section titled &quot;Consuming a scoped service in a background task&quot;):</p>
  <p><a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&amp;tabs=visual-studio" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-5.0&amp;tabs=visual-studio</a></p>
  <p>Is this correct? Do I basically need to create two services, one that does the actual work, and one that calls the service that does the actual work? Am I on the right path?</p>
  <p>Thanks</p>