static async Task Run() { var connection = new HubConnectionBuilder() .WithUrl("http://localhost:5000/signalr") .WithConsoleLogger() .WithMessagePackProtocol() .WithTransport(TransportType.WebSockets) .Build(); await connection.StartAsync(); Console.WriteLine("Starting connection. Press Ctrl-C to close."); var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender, a) => { a.Cancel = true; cts.Cancel(); }; connection.Closed += e => { Console.WriteLine("Connection closed with error: {0}", e); cts.Cancel(); return(Task.CompletedTask); }; connection.On("marketOpened", async() => { await StartStreaming(); }); // Do an initial check to see if we can start streaming the stocks var state = await connection.InvokeAsync <string>("GetMarketState"); if (string.Equals(state, "Open")) { await StartStreaming(); } // Keep client running until cancel requested. while (!cts.IsCancellationRequested) { await Task.Delay(250); } async Task StartStreaming() { var channel = connection.Stream <Stock>("StreamStocks", CancellationToken.None); while (await channel.WaitToReadAsync() && !cts.IsCancellationRequested) { while (channel.TryRead(out var stock)) { Console.WriteLine($"{stock.Symbol} {stock.Price}"); } } } }