title: How do i call a method from a singleton service to run throughout the application lifetime
body: <p>I have implemented a Kafka event bus as a singleton service in Net Core. The service itself is configured with Autofac in Startup.cs. The service has a <code>Listen()</code> method:</p>
  
  <pre><code>public void Listen()
  {
      using(var consumer = new Consumer&lt;Null, string&gt;(_config, null, new StringDeserializer(Encoding.UTF8)))
      {
          consumer.Subscribe(new string[] { "business-write-topic" });
  
          consumer.OnMessage += (_, msg) =&gt;
          {
              Console.WriteLine($"Topic: {msg.Topic} Partition: {msg.Partition} Offset: {msg.Offset} {msg.Value}");
              consumer.CommitAsync(msg);
          };
  
          while (true)
          {
              consumer.Poll(100);
          }
      }
  }
  </code></pre>
  
  <p>My understanding is that in order for this method to constantly listen for messages during the lifetime of the application, i have to call it in Program.cs from the web host by somehow getting the ServiceProvider associated with the host, then retrieving an instance of the service, and calling the method. </p>
  
  <p>I have configured my Program.cs from the default Net Core 2.1 template to the following:</p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          var host = CreateWebHost(args);
          host.Run();
      }
  
      public static IWebHost CreateWebHost(string[] args) =&gt;
          WebHost.CreateDefaultBuilder(args)
              .UseStartup&lt;Startup&gt;()
              .Build();
  }
  </code></pre>
  
  <p>Beyond having the host available so i can somehow access the services, i don't know where to go from here. I have searched for similar questions and read around in the official docs but i can't seem to figure out how to access the service so that i can call the <code>Listen()</code> method. </p>
  
  <p>Is this the "go-to" way of accomplishing my goal? If so, how do i proceed? And if not - that is - if this kind of task is commonly accomplished in another way, how do i go about it?</p>