title: Some services are not able to be constructed (Error while validating the service descriptor '...IHostedService
body: <p>I've been reading what I can find on Dependency Injection, but I can't seem to figure out what I have wrong here. I have a background service that can be set to run once a day which works fine. The background service will run daily and check if any row in a column has a date sooner than today. the issue comes when I attempt to combine it with a DB context so that it can do what I want. Here is my attempt to retrieve the table data.</p>
  <pre><code>public class GetSR : IGetSR
      {
          private readonly Template.Data.ServiceContext _context;
  
          public GetSR(Template.Data.ServiceContext context)
          {
              _context = context;
          }
  
          public IList&lt;ServiceSummary&gt; Service_Request_Informations { get; set; }
  
          public async Task GetRequests()
          {
              IQueryable&lt;ServiceSummary&gt; listIQ = from s in _context.Summary select s;
              listIQ = listIQ.Where(s =&gt; s.FollowUpDate &lt; DateTime.Now);
              Service_Request_Informations = await listIQ.AsNoTracking().ToListAsync();
          }
      }
  </code></pre>
  <p>This is the interface:</p>
  <pre><code>public interface IGetSR
  {
      Task GetRequests();
  }
  </code></pre>
  <p>Here is my background service:</p>
  <pre><code>public class ScheduledUpdate : BackgroundService
  {
      protected async override Task ExecuteAsync(CancellationToken stoppingToken)
      {
          ...
      }
  
      public ScheduledUpdate(IGetSR GetSR)
      {
          _Requests = GetSR;
      }
      public readonly IGetSR _Requests;
  }
  </code></pre>
  <p>Startup.cs</p>
  <pre><code>...
  public void ConfigureServices(IServiceCollection services)
  {
       services.AddRazorPages();
  
       services.AddTransient&lt;IGetSR, GetSR&gt;();
  ...
  }
  ...
  </code></pre>
  <p>My code will not build anymore. If I understand DI correctly, I should not need to scope IGetSR to BackgroundService like I do GetSR, yet it says it cannot be scoped. What am I doing wrong here?</p>
  <p>SOLVED: Thanks to Compufreak. I needed to make some adjustments to work for me</p>
  <pre><code>protected async override Task ExecuteAsync(CancellationToken stoppingToken)
  {
      using var scope = _ServiceProvider.CreateScope();
      var requests = scope.ServiceProvider.GetRequiredService&lt;IGetSR&gt;();
  
      var DailyTime = &quot;15:00:00&quot;;
      var timeParts = DailyTime.Split(new char[1] { ':' });
  
      while (!stoppingToken.IsCancellationRequested)
      {
          var dateNow = DateTime.Now;
          var date = new DateTime(dateNow.Year, dateNow.Month, dateNow.Day, int.Parse(timeParts[0]), int.Parse(timeParts[1]), int.Parse(timeParts[2]));
          TimeSpan ts;
  
          if (date &gt; dateNow)
          {
              ts = date - dateNow;
          }
          else
          {
              date = date.AddDays(1);
              ts = date - dateNow;
          }
  
          await Task.Delay(ts, stoppingToken);
          await requests.GetRequests();
      }
  }
  </code></pre>