Home > Back-end >  DI and Classes without DI how to instantiate the DB
DI and Classes without DI how to instantiate the DB

Time:04-17

I have a razor pages with some classes such as scheduled tasks that run in the background. I have a IUnitofWork for the databases and uses EF.

In my schedule class "WorkerService : BackgroundService" it does routine backups and other tasks.

How can I reference the Database because I dont have DI due to not implementing razor pages?

Usually this is how I do it using DI on razor code files:

private readonly IUnitOfWork _unitOfWork;
public IndexModel(IUnitOfWork unitOfWork)
{
    _unitOfWork = unitOfWork;
}

I am new to this DI and been in the udemy and microsoft site daily. I think I have to create a IUnit of work and pass in the ApplicationDbContext maybe in an ovveride? But how to get the context without DI.

Program.cs

builder.Services.AddHostedService<WorkerService>(); //Uses cronos to execute DoWork() every hour

WorkerService.cs

private const string schedule = "*/5 * * * *"; // every 5 for testing
private readonly CronExpression _cron;

public WorkerService()
{
    _cron = CronExpression.Parse(schedule);
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        var utcNow = DateTime.UtcNow;
        var nextUtc = _cron.GetNextOccurrence(utcNow);
        await Task.Delay(nextUtc.Value - utcNow, stoppingToken);
        await DoBackupAsync();
    }
}
private static Task DoBackupAsync()
{
    DoWork d = new DoWork();
    return Task.FromResult("Done");
}

RazorApp/Pages

This is where I need to save data

RazorApp/ScheduledTasks/DoWork.cs

RazorApp/ScheduledTasks/WorkerService.cs

Attempting to DI either the IUnitOfWork or ApplicationDbContext

Further trying different examples like: https://dotnetcorecentral.com/blog/background-tasks

Results in this error as well: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: WebRazor.ScheduledTasks.BackgroundPrinter': Cannot consume scoped service 'WebRazor.DataAccess.ApplicationDbContext' from singleton 'Microsoft.Extensions.Hosting.IHostedService

public BackgroundPrinter(ILogger<BackgroundPrinter> logger, IWorker worker, ApplicationDbContext dbContext)
        {
            this.logger = logger;
            applicationDbContext = dbContext;
        }

Is this where I need to get it from the settings directly or is there a slick way to grab the Db Context?

CodePudding user response:

Ok, wow I was all over the place. I implemented this and it worked.

public BackgroundPrinter(ILogger<BackgroundPrinter> logger, IWorker worker, IServiceProvider serviceProvider)
        {
            this.logger = logger;
            unitOfWork = serviceProvider.CreateScope().ServiceProvider.GetRequiredService<IUnitOfWork>();
        }

On construct pass in the Iserviceprovider and access this way. DI to the DoWork and its writing to the database.

Still not sure this is the best way to solve this problem, but I think I have to refactor my unitofwork because every now and then I get an error on writing "A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext" but this may be easy to solve.

CodePudding user response:

The background tasks with hosted services doc describes how to consume a scoped service in a background task:

  • Inject IServiceProvider in hosted service's ctor
  • Use the IServiceProvider to create scope during the execution
  • Use the scope to resolve required services

Personally for "timed" services like provided WorkerService in most cases I found useful to create scope per every iteration (especially if it uses EF internally). Something along this lines:

private readonly IServiceProvider _serviceProvider;
 
public WorkerService(IServiceProvider serviceProvider)
{
    ...
    _serviceProvider = serviceProvider;
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
    while (!stoppingToken.IsCancellationRequested)
    {
        var utcNow = DateTime.UtcNow;
        var nextUtc = _cron.GetNextOccurrence(utcNow);
        await Task.Delay(nextUtc.Value - utcNow, stoppingToken);
        await DoBackupAsync(stoppingToken);
    }
}

private static async Task DoBackupAsync(CancellationToken stoppingToken)
{
    using (var scope = _serviceProvider.CreateScope()) // do not forget to dispose the scope
    {
        var backupService = scope.ServiceProvider.GetRequiredService<IBackupService>();

        await backupService.BackUp(stoppingToken);
    }
}
  • Related