Short running background task in .NET Core : <p>I just discovered <code>IHostedService</code> and .NET Core 2.1 <code>BackgroundService</code> class. I think idea is awesome. <a href="https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/background-tasks-with-ihostedservice" rel="noreferrer">Documentation</a>.</p>

<p>All examples I found are used for long running tasks (until application die).
But I need it for short time. Which is the correct way of doing it?</p>

<p><strong>For example:</strong><br>
I want to execute a few queries (they will take approx. 10 seconds) after application starts. And only if in development mode. I do not want to delay application startup so <code>IHostedService</code> seems good approach. I can not use <code>Task.Factory.StartNew</code>, because I need dependency injection.</p>

<p>Currently I am doing like this:</p>

<pre><code>public class UpdateTranslatesBackgroundService: BackgroundService
{
    private readonly MyService _service;

    public UpdateTranslatesBackgroundService(MyService service)
    {
        //MService injects DbContext, IConfiguration, IMemoryCache, ...
        this._service = service;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        await ...
    }
}
</code></pre>

<p>startup:</p>

<pre><code>public static IServiceProvider Build(IServiceCollection services, ...)
{
    //.....
    if (hostingEnvironment.IsDevelopment())
        services.AddSingleton&lt;IHostedService, UpdateTranslatesBackgroundService&gt;();
    //.....
}
</code></pre>

<p>But this seems overkill. Is it? Register singleton (that means class exists while application lives). I don't need this. Just create class, run method, dispose class. All in background task.</p>
