private HubConnection BuildHubConnection(HttpTransportType transportType, CancellationToken cancellationToken) { var url = _sharedSettings.BaseUrl + "remoteagent"; // create a new connection that points to web server. var con = new HubConnectionBuilder() .WithUrl(url, options => { options.Transports = transportType; options.Cookies = _sharedSettings.CookieContainer; }) .AddJsonProtocol(options => { options.PayloadSerializerOptions.Converters.Add(new JsonObjectConverter()); options.PayloadSerializerOptions.Converters.Add(new JsonTimeSpanConverter()); options.PayloadSerializerOptions.Converters.Add(new JsonDateTimeConverter()); }) .ConfigureLogging(logging => { logging.SetMinimumLevel(_remoteSettings.Logging.LogLevel.Default); }) .Build(); // // call the "ProcessMessage" function whenever a signalr message is received. // con.On<RemoteMessage>("Command", async message => // { // await ProcessMessage(message); // }); //TODO Implement process message without response message. con.On <RemoteMessage>("Command2", async message => { await ProcessMessage(message); }); con.On <RemoteMessage>("Response", message => { _messageQueue.AddResponse(message.MessageId, message); }); // signals the agent is alive, and sends a response message back to the calling client. con.On <string>("Ping", async connectionId => { await _hubConnection.SendAsync("Ping", GetActiveAgent(), connectionId, cancellationToken); }); // signals the agent is alive, and sends a response message back to the calling client. con.On <string>("PingServer", async pingKey => { await _hubConnection.SendAsync("PingServer", GetActiveAgent(), pingKey, cancellationToken); }); con.On <string>("Restart", async => { _remoteOperations.ReStart(null, cancellationToken); }); con.On("Abort", async() => { _logger.LogInformation("Listener connection aborted."); _sharedSettings.ResetConnection(); await con.StopAsync(cancellationToken); }); // when closed cancel and exit con.Closed += async e => { _sharedSettings.ResetConnection(); if (e == null) { _logger.LogInformation("Listener connection closed."); } else { _logger.LogError("Listener connection closed with error: {0}", e.Message); } if (!cancellationToken.IsCancellationRequested) { // if closed, then attempt to reconnect. await Connect(cancellationToken); } }; return(con); }