title: MassTransit 5.2, SignalR: How can i get the IHubContext inside my Consumer?
body: <p>My main problem is to get the right instance of the SignalR hub.</p>
  
  <p>Context: Im building a webapplication which communicates with a couple of external systems. CRUD operations in my application result in updating the databases of the external systems. </p>
  
  <p>In this example i have 3 services running:</p>
  
  <p>ExternalSystem | StateMachine | .NET CORE WebAPI</p>
  
  <p>When i post the 'create employee' form, a RabbitMQ message will be sent from the WebAPI to the statemachine. The statemachine then sends a couple of create messages to my external system service which updates the database. Thereafter, it updates the statemachine to keep track of the createoperation.</p>
  
  <p>Form -> API -> StateMachine -> ExternalSystem -> StateMachine -> API</p>
  
  <p>So far so good. Now i would like to use SignalR to send the status updates to the client. So i've implemented this consumer in the API:</p>
  
  <pre><code>public class UpdatesConsumer :
      IConsumer&lt;IExternalSystemUpdateMessage&gt;
  {
      private readonly IHubContext&lt;UpdatesHub&gt; _updaterHubContext;
  
      public UpdatesConsumer(IHubContext&lt;UpdatesHub&gt; hubContext)
      {
          _updaterHubContext = hubContext;
      }
  
      public Task Consume(ConsumeContext&lt;IExternalSystemUpdateMessage&gt; context)
      {
          //return _updaterHubContext.Clients.Group(context.Message.CorrelationId.ToString()).SendAsync("SEND_UPDATE", context.Message.Message);// this.SendUpdate(context.Message.CorrelationId, context.Message.Message);
          return _updaterHubContext.Clients.All.SendAsync("SEND_UPDATE", context.Message.Message);
      }
  }
  </code></pre>
  
  <p>This is my SignalR hub:</p>
  
  <pre><code>public class UpdatesHub :
      Hub
  {
      public Task SendUpdate(Guid correlationId, string message)
      {
          return Clients.Group(correlationId.ToString()).SendAsync("SEND_UPDATE", message);
      }
  }
  </code></pre>
  
  <p>And this is how the Bus and consumer is instantiated:</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          _services = services;
  
          services.AddMvc();
          services.AddSignalR();            
          //services.AddSingleton&lt;IHubContext&lt;UpdatesHub&gt;&gt;();
  
          WebAPI.CreateBus();
      }
  
      public static IServiceCollection _services;
  
      static IBusControl _busControl;
      public static IBusControl Bus
      {
          get
          {
              return _busControl;
          }
      }
  
      public static void CreateBus()
      {
          IRMQConnection rmqSettings = Config.GetRMQConnectionConfig("rmq-settings.json", "connection");
  
          _busControl = MassTransit.Bus.Factory.CreateUsingRabbitMq(x =&gt;
          {
              var host = x.Host(BusInitializer.GetUri("", rmqSettings), h =&gt;
              {
                  h.Username(rmqSettings.UserName);
                  h.Password(rmqSettings.Password);
              });
  
              x.ReceiveEndpoint(host, "externalsystems.update",
                  e =&gt; { e.Consumer(() =&gt; new UpdatesConsumer((IHubContext&lt;UpdatesHub&gt;)Startup.__serviceProvider.GetService(typeof(IHubContext&lt;UpdatesHub&gt;)))); });
          });
  
          TaskUtil.Await(() =&gt; _busControl.StartAsync());
      }
  </code></pre>
  
  <p>=========================================================================</p>
  
  <p>So the problem is that _updaterHubContext.Clients in my Consumer class, always turn out to be empty. I've tested accessing the hub in a controller, and the clients do show up:</p>
  
  <pre><code>public class TestController : Controller
  {
      private readonly IHubContext&lt;UpdatesHub&gt; _hubContext;
      public TestController(IHubContext&lt;UpdatesHub&gt; hubContext)
      {
          _hubContext = hubContext;
      }
  
      [HttpGet]
      [Route("api/Test/")]
      public IActionResult Index()
      {
          return View();
      }
  }
  </code></pre>
  
  <p>How can i get the right instance of the hub in my Consumer class? Or how can i access the IServiceCollection that .net is using?</p>
  
  <p>Thnx in advance!</p>