title: ASP.NET Core 6 - How do I get required service
body: <p>I'm following the example for queued services on <a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/queue-service#rewrite-the-worker-class" rel="nofollow noreferrer">MSDN</a> but I don't know how to get an instance for <code>MonitorLoop</code>, because <code>host.Services.GetRequiredService</code> is undefined. How can I retrieve it in ASP.NET Core 6?</p>
  <p>Or may a better practice be to use a background scoped service for the <code>MonitorLoop</code>? <a href="https://docs.microsoft.com/en-us/dotnet/core/extensions/scoped-service" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/core/extensions/scoped-service</a></p>
  <pre class="lang-cs prettyprint-override"><code>public static class QueueServiceExtensions
  {
      public static void AddQueueService(this IServiceCollection services, IConfiguration configuration)
      {
          services.AddSingleton&lt;MonitorLoop&gt;();
          services.AddHostedService&lt;QueuedHostedService&gt;();
          services.AddSingleton&lt;IBackgroundTaskQueue&gt;(_ =&gt;
          {
              if (!int.TryParse(configuration[&quot;QueueCapacity&quot;], out var queueCapacity))
              {
                  queueCapacity = 100;
              }
  
              return new DefaultBackgroundTaskQueue(queueCapacity);
          });
          
          // TODO: host.Services is undefined
          MonitorLoop monitorLoop = host.Services.GetRequiredService&lt;MonitorLoop&gt;()!;
          monitorLoop.StartMonitorLoop();
      }
  }
  </code></pre>
  <p>This is how my <em>Program.cs</em> looks like</p>
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.AspNetCore.Authentication.JwtBearer;
  using Microsoft.Identity.Web;
  using QSGEngine.Web.QueueService;
  using QSGEngine.Web.SignalR;
  using Serilog;
  
  var builder = WebApplication.CreateBuilder(args);
  
  // Logging
  builder.Host.UseSerilog((context, configuration) =&gt;
  {
      configuration
          .ReadFrom.Configuration(context.Configuration)
          .Enrich.FromLogContext()
          .Enrich.WithMachineName()
          .Enrich.WithProperty(&quot;Environment&quot;, Environment.GetEnvironmentVariable(&quot;ASPNETCORE_ENVIRONMENT&quot;))
          .WriteTo.Console(
  #if DEBUG
              restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
  #else
              restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Information,
  #endif
              outputTemplate: builder.Configuration[&quot;Serilog:OutputTemplateConsole&quot;]
          )
          .WriteTo.File(
              path: builder.Configuration[&quot;Serilog:LogFileLocation&quot;], retainedFileCountLimit: 7, rollingInterval: RollingInterval.Day, buffered: false,
              outputTemplate: builder.Configuration[&quot;Serilog:OutputTemplateFile&quot;]
          );
  });
  
  // CORS
  builder.Services.AddCors(options =&gt;
  {
      options.AddPolicy(&quot;CorsPolicy&quot;, configurePolicy =&gt; configurePolicy
          .WithOrigins(builder.Configuration[&quot;Configuration:FrontEndUrl&quot;])
          .AllowAnyMethod()
          .AllowAnyHeader()
          .AllowCredentials());
  });
  
  // Authentication &amp; Authorization
  builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
      .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection(&quot;AzureAd&quot;));
  
  // Services
  //builder.Services.AddHostedService&lt;PortfolioService&gt;();
  builder.Services.AddQueueService(builder.Configuration);
  
  builder.Services.AddControllers();
  
  // Swagger
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddSwaggerGen();
  
  // SignalR
  //builder.Services.AddSignalR();
  builder.Services.AddPortfolioSignalRService();
  
  var app = builder.Build();
  
  if (app.Environment.IsDevelopment())
  {
      // Swagger
      app.UseSwagger();
      app.UseSwaggerUI();
  }
  
  app.UseHttpsRedirection();
  
  // CORS
  app.UseCors(&quot;CorsPolicy&quot;);
  
  // Web Sockets
  var webSocketOptions = new WebSocketOptions()
  {
      KeepAliveInterval = TimeSpan.FromSeconds(120)
  };
  app.UseWebSockets(webSocketOptions);
  
  // Authentication &amp; Authorization
  app.UseAuthentication();
  app.UseAuthorization();
  
  app.MapControllers();
  
  // SignalR
  //app.MapHub&lt;PortfolioHub&gt;(&quot;/portfolios&quot;);
  app.MapPortfolioSignalR();
  
  app.Run();
  
  </code></pre>