2

I've got an API built using ASP.NET Core 2.1 that does some basic CRUD operations.

This is some sort of a release pipeline application, dedicated to our internal processes. It works as follows:

  1. User requests a data reload (POST)
  2. User approves a data reload (POST)
  3. User kicks off SSIS package in database that will trigger that data reload (POST)
  4. Background process checks status of that SSIS package and when it completes will set state state of reload to done/failed

Our initial idea was to create a background service that would periodically query database and check what's the current status of SSIS package and then update our entity to desired state (done/failed).

However this does not work because every other service is scoped while background service is a singleton.

What could be an alternative to run that background service in order to maintain correct state of my entity? A separate project?

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
  • 1
    background service is a hosted service , a hosted service can be injected with a `IServiceProvider` . As a result , you can create a new scope , in which you can reference a new scoped service safely . – itminus Sep 03 '18 at 08:17
  • Possible duplicate of [How should I inject a DbContext instance into an IHostedService?](https://stackoverflow.com/questions/48368634/how-should-i-inject-a-dbcontext-instance-into-an-ihostedservice) – alsami Sep 03 '18 at 08:40
  • 1
    I marked it as duplicate because that has been answered before. Please use IServiceScopeFactory instead of IServiceProvider. – alsami Sep 03 '18 at 08:40
  • @alsami if you feel the need to mark a post as answered before and state it in the comments then in the very least you'd do well to post a link to where you saw this issue being discussed previously. Right now this very post is the first stack overflow search hit on "asp.net core background services", and in the light of this your comment looks a bit contradictory and unhelpful. – RAM Nov 27 '18 at 13:53
  • For those arriving at this question lookign for ASP.NET background services, here's an interesting read on [implementing IHostedServices](https://www.stevejgordon.co.uk/asp-net-core-2-ihostedservice). – RAM Nov 27 '18 at 14:01
  • @RAM I did that in one of the previous comments (which is only one demonstration). You will find many stackoverflow posts about background-tasks as well. – alsami Nov 27 '18 at 14:50

1 Answers1

0

I recently created a back ground service in my API and the below solution work for me:

  1. In your background service class constructor add IServiceProvider as a dependency:

    private readonly IServiceProvider _serviceProvider;
    
    public MyBackgoundService(IServiceProvider serviceProvider)
    {
         _serviceProvider = serviceProvider;
    
    }
    
  2. In your implementation code, use the service provider to inject your database context or repository

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {        
        while(!stoppingToken.IsCancellationRequested){
    
                using var scope = _serviceProvider.CreateScope();
                var context = scope.ServiceProvider.GetRequiredService<MyDbContext>();
    
         }
    }
    
MG89
  • 31
  • 3