title: Instantiating IHubContext in ASP.NET Core
body: <p>I am using SignalR on different places of my web project. In my Controllers and HostedService this seems to be working fine. Clients instantiate connections with my hub and I can communicate with them back using an IHubContext instance, injected in the constructor of every controller/hostedservice.</p>
  
  <p>I am having another singleton, running in Background (No HosteService or BackgroundTask). This class is getting IHubContext injected in the constructor also. Still every time it gets called, it seems like this singleton is having a different instance of IHubContext, since this context has no clients/groups connected to it.</p>
  
  <p>This class is being registered as this in the startup function:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddSingleton&lt;IServiceEventHandler&lt;TourMonitorEvent&gt;, TourMonitorEventHandler&gt;();
  </code></pre>
  
  <p>To configure SignalR I am doing the following in ConfigureServices:</p>
  
  <pre class="lang-cs prettyprint-override"><code>services.AddSignalR().AddNewtonsoftJsonProtocol();
  </code></pre>
  
  <p>and the following in configure:</p>
  
  <pre class="lang-cs prettyprint-override"><code>app.UseEndpoints(endpoints =&gt;
  {
      endpoints.MapHub&lt;MyHubClass&gt;("/hubEndpoint");
      endpoints.MapControllers();
  });
  </code></pre>
  
  <p>the IHubContext ist bein injected as following in both Controllers/Hostedservices and the singleton:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MySingleton : IHandler&lt;SomeGenericClass&gt;
  {
      private readonly IHubContext&lt;MyHubClass&gt; _hubContext;
  
      public MySingleton(IHubContext&lt;MyHubClass&gt; hubContext)
      {
          _hubContext = hubContext;
      } 
  }
  </code></pre>
  
  <p>Are Controllers/HosteService being instantiated differently than my Singleton, in a way that might affect IHubContext instantiation?</p>