title: Running a Singleton instance method on app start up using dependency injection dotnet core
body: <p>I have a generated webapi .NET Core application with a <code>Queue</code> class that continuously loops through a <code>List&lt;Item&gt;</code> object where an <code>Item</code> is a wrapper around a data model and includes some metadata to do some task asynchronously. This <code>Queue</code> class needs to be accessible in a controller and I have it registered with the DI framework (I think) properly. My issue is that I need for the <code>BeginProcessing</code> method to begin running when the application starts and for the instance passed to the controller to be the same as the one running the method and I am unsure the best route to go. Any help is appreciated. Anything not included in this context (i.e. <code>Program</code>, etc. is all pretty much default still.</p>
  
  <p>See Below.</p>
  
  <p><strong>Queue.cs:</strong></p>
  
  <pre><code>public class Queue : IQueue
  {
      public List&lt;Item&gt; Items { get; set; }
  
      public void BeginProcessing()
      {
          while(true)
          {
              if (!(Items.Count &lt; 1))
              {
                  Item Item = Items[0];
                  if (!Item.Push())
                  {
                      Console.WriteLine("Generic Error Message, not final implementation.");
                  }
                  Items.Remove(Item);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Item.cs</strong></p>
  
  <pre><code>public class Item
  {
      public SomeDataModel Data { get; set; } 
      public string Topic { get; set; }
      public string Timestamp { get; set; }
  
      public Item(SomeDataModel newData, string newTopic, string newTimestamp)
      {
          Data = newData;
          Topic = newTopic;
          Timestamp = newTimestamp;
      }
  
      public bool Push()
      {
          Producer p = new Producer("identifier");
          return p.Ingest(Data);
      }
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
      {
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  
          services.AddSingleton&lt;IQueue, Queue&gt;();
      }
  </code></pre>