title: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType:IHostedService
body: <p>I am using .NET 5 and I want to run a background task using <code>IHostedService</code> classess as you can see:</p>
  <pre class="lang-cs prettyprint-override"><code>public class PasargadJobs : IHostedService, IDisposable
  {
      private Timer _timer = null!;
      readonly  ITerminalService _terminalService;
      readonly  IMessageService _messageService;
      readonly IMerchantService _merchantService;
      
      public PasargadJobs(
          IMerchantService merchantService,
          ITerminalService terminalService,
          IMessageService messageService)
      {
          _messageService = messageService;
          _terminalService = terminalService;
          _merchantService = merchantService;
      }
      
      //other parts of code are removed for short code
  }
  </code></pre>
  <p>So I have to inject it to service collection as you can see :</p>
  <pre class="lang-cs prettyprint-override"><code>services.AddHostedService&lt;PasargadJobs&gt;();
  </code></pre>
  <p>But after adding this I got this error :</p>
  <blockquote>
  <p>System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: MIMS.Portal.ScheduleJobs.PasargadJobs': Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions`1[MIMS.Portal.Infrustructure.Repositories.AppDbContext]' from singleton 'Microsoft.Extensions.Hosting.IHostedService'.)'</p>
  </blockquote>
  <p>Here is my startup.cs</p>
  <pre class="lang-cs prettyprint-override"><code>services.AddExpressiveAnnotations();
  
  //--- DB Context ---//
  services.AddTransient&lt;AppDbContext, AppDbContext&gt;();
  //--- Repositories ---//
  services.AddTransient(typeof(IGenericRepository&lt;&gt;), typeof(GenericRepository&lt;&gt;));
  services.AddTransient&lt;IMerchantRepository, MerchantRepository&gt;();
  services.AddTransient&lt;IBankAccountRepository, BankAccountRepository&gt;();
  services.AddTransient&lt;IBankRepository, BankRepository&gt;();
  services.AddTransient&lt;IGeoRepository, GeoRepository&gt;();
  services.AddTransient&lt;IDepartmentRepository, DepartmentRepository&gt;();
  services.AddTransient&lt;IDeviceRepository, DeviceRepository&gt;();
  services.AddTransient&lt;IInvoiceRepository, InvoiceRepository&gt;();
  services.AddTransient&lt;IStoreRepository, StoreRepository&gt;();
  services.AddTransient&lt;ITerminalRepository, TerminalRepository&gt;();
  services.AddTransient&lt;ITicketRepository, TicketRepository&gt;();
  services.AddTransient&lt;IUserRepository, UserRepository&gt;();
  services.AddTransient&lt;IFileRepository, FileRepository&gt;();
  services.AddTransient&lt;IPartnerRepository, PartnerRepository&gt;();
  services.AddTransient&lt;IStoreScopeRepository, StoreScopeRepository&gt;();
  services.AddTransient&lt;ICommentRepository, CommentRepository&gt;();
  services.AddTransient&lt;INewsRepository, NewsRepository&gt;();
  services.AddTransient&lt;IPosBrandRepository, PosBrandRepository&gt;();
  //-- Services --//
  services.AddTransient&lt;IMerchantService, MerchantService&gt;();
  services.AddTransient&lt;IBankAccountService, BankAccountService&gt;();
  services.AddTransient&lt;IBankService, BankService&gt;();
  services.AddTransient&lt;IGeoService, GeoService&gt;();
  services.AddTransient&lt;IDepartmentService, DepartmentService&gt;();
  services.AddTransient&lt;IDeviceService, DeviceService&gt;();
  services.AddTransient&lt;IInvoiceService, InvoiceService&gt;();
  services.AddTransient&lt;IStoreService, StoreService&gt;();
  services.AddTransient&lt;ITerminalService, TerminalService&gt;();
  services.AddTransient&lt;ITicketService, TicketService&gt;();
  services.AddTransient&lt;IUsersService, UsersService&gt;();
  services.AddTransient&lt;IFilesManagerService, FilesManagerService&gt;();
  services.AddTransient&lt;IMessageService, MessageService&gt;();
  services.AddTransient&lt;IPartnerService, PartnerService&gt;();
  services.AddTransient&lt;IStoreScopeService, StoreScopeService&gt;();
  services.AddTransient&lt;INewsService, NewsService&gt;();
  services.AddTransient&lt;ICommentService, CommentService&gt;();
  services.AddTransient&lt;IPosBrandService, PosBrandService&gt;();
  
  services.AddSingleton&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
  
  services
      .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
      .AddJwtBearer(option =&gt;
      {
          option.TokenValidationParameters = new TokenValidationParameters
          {
              ValidateIssuer = true,
              ValidateAudience = true,
              ValidateLifetime = true,
              ValidateIssuerSigningKey = true,
              ValidIssuer = &quot;localhost&quot;,
              ValidAudience = &quot;localhost&quot;,
              IssuerSigningKey = new SymmetricSecurityKey(
                  Encoding.UTF8.GetBytes(&quot;wevhgfrtyhasdfghjklzxcvbnm&quot;))
          };
      });
      
  services.AddHostedService&lt;PasargadJobs&gt;();
  </code></pre>