title: DI and Classes without DI how to instantiate the DB
body: <p>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.</p>
  <p>In my schedule class &quot;WorkerService : BackgroundService&quot; it does routine backups and other tasks.</p>
  <p>How can I reference the Database because I dont have DI due to not implementing razor pages?</p>
  <p>Usually this is how I do it using DI on razor code files:</p>
  <pre><code>private readonly IUnitOfWork _unitOfWork;
  public IndexModel(IUnitOfWork unitOfWork)
  {
      _unitOfWork = unitOfWork;
  }
  </code></pre>
  <p>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.</p>
  <p>Program.cs</p>
  <pre><code>builder.Services.AddHostedService&lt;WorkerService&gt;(); //Uses cronos to execute DoWork() every hour
  </code></pre>
  <p><strong>WorkerService.cs</strong></p>
  <pre><code>private const string schedule = &quot;*/5 * * * *&quot;; // 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(&quot;Done&quot;);
  }
  </code></pre>
  <p>RazorApp/Pages</p>
  <p><strong>This is where I need to save data</strong></p>
  <p>RazorApp/ScheduledTasks/DoWork.cs</p>
  <p>RazorApp/ScheduledTasks/WorkerService.cs</p>
  <p><strong>Attempting to DI either the IUnitOfWork or ApplicationDbContext</strong></p>
  <p>Further trying different examples like: <a href="https://dotnetcorecentral.com/blog/background-tasks" rel="nofollow noreferrer">https://dotnetcorecentral.com/blog/background-tasks</a></p>
  <p>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</p>
  <pre><code>public BackgroundPrinter(ILogger&lt;BackgroundPrinter&gt; logger, IWorker worker, ApplicationDbContext dbContext)
          {
              this.logger = logger;
              applicationDbContext = dbContext;
          }
  </code></pre>
  <p>Is this where I need to get it from the settings directly or is there a slick way to grab the Db Context?</p>