title: How to start HostedService in MVC Core app without http request
body: <p>In my MVC .NET core 2.2 app there is HostedService which doing background work.</p>
  
  <p>It is register in ConfigureServices method of Startap class</p>
  
  <pre><code>services.AddHostedService&lt;Engines.KontolerTimer&gt;();
  </code></pre>
  
  <p>Since this is background service independent of users requests I want to  start my background service immediately when app starts.
  Now is case to my HostedService staring after first user request.</p>
  
  <p>What is proper way to start HostedService when MVC Core app start</p>
  
  <p>My serivce looks like this one <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2" rel="noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-2.2</a></p>
  
  <pre><code>internal class TimedHostedService : IHostedService, IDisposable
  {
      private readonly ILogger _logger;
      private Timer _timer;
  
      public TimedHostedService(ILogger&lt;TimedHostedService&gt; logger)
      {
          _logger = logger;
      }
  
      public Task StartAsync(CancellationToken cancellationToken)
      {
          _logger.LogInformation("Timed Background Service is starting.");
  
          _timer = new Timer(DoWork, null, TimeSpan.Zero, 
              TimeSpan.FromSeconds(5));
  
          return Task.CompletedTask;
      }
  
      private void DoWork(object state)
      {
          _logger.LogInformation("Timed Background Service is working.");
      }
  
      public Task StopAsync(CancellationToken cancellationToken)
      {
          _logger.LogInformation("Timed Background Service is stopping.");
  
          _timer?.Change(Timeout.Infinite, 0);
  
          return Task.CompletedTask;
      }
  
      public void Dispose()
      {
          _timer?.Dispose();
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Looks like I have problem  staring app at all.</p>
  
  <p>My porgram cs looks like</p>
  
  <pre><code>public class Program
      {
          public static void Main(string[] args)
          {
             CreateWebHostBuilder(args).Build().Run();
  
  
          }
  
          public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;
              WebHost.CreateDefaultBuilder(args)
              .UseSerilog((ctx, config) =&gt; { config.ReadFrom.Configuration(ctx.Configuration); })
              .UseStartup&lt;Startup&gt;();
      }
  </code></pre>
  
  <p>And I do not hit any break point before first user request.
  Am I miss something, this is default .Net Core app created by VS2017</p>
  
  <p>Here is my starup.cs </p>
  
  <pre><code>public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
          private Models.Configuration.SerialPortConfiguration serialPortConfiguration;
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
              {
                  // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                  options.CheckConsentNeeded = context =&gt; true;
                  options.MinimumSameSitePolicy = SameSiteMode.None;
              });
  
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(
                      Configuration.GetConnectionString("DefaultConnection")));
  
              services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;(options =&gt; options.Stores.MaxLengthForKeys = 128)
                  .AddDefaultUI(UIFramework.Bootstrap4)
                  .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
                  .AddDefaultTokenProviders();
  
              services.AddDbContext&lt;Data.Parking.parkingContext&gt;(options =&gt;
                  options.UseSqlServer(
                      Configuration.GetConnectionString("DefaultConnection")));
  
  
           services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
              services.AddHostedService&lt;Engines.KontolerTimer&gt;();}
  </code></pre>