title: Invoking SignalR Hub not working for Asp.Net Core Web API
body: <p>I'm a newb to SignalR. I'm trying to set up a Asp.Net Core WebAPI so that other clients can connect to it using SignalR and get real-time data.
  My Hub class is:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class TimeHub : Hub
  {
      public async Task UpdateTime(string message)
      {
          await Clients.All.SendAsync("ReceiveMessage", message);
      }
  }
  </code></pre>
  
  <p>I have a relay class as follows:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class TimeRelay : ITimeRelay
  {
      private readonly IHubContext&lt;TimeHub&gt; _timeHubContext;
  
      public TimeRelay(IHubContext&lt;TimeHub&gt; context)
      {
  
          _timeHubContext = context;
          Task.Factory.StartNew(async () =&gt;
          {
              while (true)
              {
                  await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());
                  Thread.Sleep(2000);
              }
          });
      }
  }
  </code></pre>
  
  <p>Startup class: </p>
  
  <pre class="lang-cs prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
      services.AddSignalR();
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
      app.UseDeveloperExceptionPage();
  
      app.UseHttpsRedirection();
  
      app.UseSignalR((x) =&gt;
      {
          x.MapHub&lt;TimeHub&gt;("/timeHub");
      });
      app.UseMvc();
  }
  </code></pre>
  
  <p>The client is a console application and the code is:</p>
  
  <pre class="lang-cs prettyprint-override"><code>class Program
  {    
      static Action&lt;string&gt; OnReceivedAction = OnReceived;
  
      static void Main(string[] args)
      {
          Connect();
          Console.ReadLine();
      }
  
      private static async void Connect()
      {
          var hubConnectionBuilder = new HubConnectionBuilder();
  
          var hubConnection = hubConnectionBuilder.WithUrl("http://localhost:60211/timeHub").Build();
  
          await hubConnection.StartAsync();
  
          var on = hubConnection.On("ReceiveMessage", OnReceivedAction);
  
          Console.ReadLine();    
          on.Dispose();
          await hubConnection.StopAsync();
      }
  
      static void OnReceived(string message)
      {
          System.Console.WriteLine($"{message}");
      }
  }
  </code></pre>
  
  <p>I tried debugging the application. The client got connected to the <code>TimeHub</code> succesfully. The no of connections in <code>Clients.All</code> changed from 0 to 1, when the client got connected. But, when <code>await context.Clients.All.SendAsync("UpdateTime", DateTime.Now.ToShortDateString());</code> is executed, the <code>UpdateTime</code> function in <code>TimeHub</code> is not getting executed and the client is not getting any message.</p>
  
  <p>I tried using <code>"UpdateTime"</code>, <code>"SendMessage"</code>, and <code>"ReceiveMessage"</code> as method in <code>Clients.All.SendAsync</code> in <code>TimeRelay</code> class. Nothing worked. Could someone point out my mistake in this.</p>