title: Is there a way to run a DI scoped service on a timer with .net core?
body: <p>I am writing a .net core web-api backend and want to run a code that collects and writes statistics to my database every 4 hours.</p>
  
  <p>In order to do that I need to inject my DB context into some method and run it every 4 hours with a timer, Problem is i can not inject the DB to this static method.</p>
  
  <p>I have tried using ActivatorUtilities.CreateInstance but again i can not get IServiceProvider since i also need to inject it.</p>
  
  <p>This is roughly what im trying to get running periodically</p>
  
  <pre class="lang-cs prettyprint-override"><code>    public class StatisticsProvider : IStatisticsProvider
      {
          private readonly AppCtx _appCtx;
  
          public StatisticsProvider(AppCtx appCtx)
          {
              _appCtx = appCtx;
          }
  
          public void LogStatistics()
          {
              //get data from database and generate statistics 
              //Write statistics to DB
          }
     }
  
  </code></pre>
  
  <p>Thanks!</p>