Skip to content

Instantly share code, notes, and snippets.

@poojarsn
Last active September 17, 2024 02:46

Revisions

  1. poojarsn revised this gist Sep 17, 2024. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions DIscoped.cs
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    public class BackgroundJob(IServiceScopeFactory serviceScopeFactory)
    : BackgroundService
    public class SomeSingletonService(IServiceScopeFactory serviceScopeFactory)
    : SingletonService
    {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
  2. poojarsn created this gist Sep 17, 2024.
    15 changes: 15 additions & 0 deletions DIscoped.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    public class BackgroundJob(IServiceScopeFactory serviceScopeFactory)
    : BackgroundService
    {
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
    using IServiceScope scope = serviceScopeFactory.CreateScope();

    var dbContext = scope
    .ServiceProvider
    .GetRequiredService<ApplicationDbContext>();

    // Do some background processing with the EF database context.
    await DoWorkAsync(dbContext);
    }
    }