public async Task OnInvocationAsync(HubConnectionContext connection, HubMethodInvocationMessage message, bool isStreamedInvocation) { try { // If an unexpected exception occurs then we want to kill the entire connection // by ending the processing loop if (!_methods.TryGetValue(message.Target, out var descriptor)) { // Send an error to the client. Then let the normal completion process occur _logger.UnknownHubMethod(message.Target); await SendMessageAsync(connection, CompletionMessage.WithError( message.InvocationId, $"Unknown hub method '{message.Target}'")); } else { await Invoke(descriptor, connection, message, isStreamedInvocation); } } catch (Exception ex) { // Abort the entire connection if the invocation fails in an unexpected way connection.Abort(ex); } }
private async Task ProcessInvocation(HubConnectionContext connection, InvocationMessage invocationMessage) { try { // If an unexpected exception occurs then we want to kill the entire connection // by ending the processing loop await Execute(connection, invocationMessage); } catch (Exception ex) { // Abort the entire connection if the invocation fails in an unexpected way connection.Abort(ex); } }
public async Task OnConnectedAsync(ConnectionContext connection) { var output = Channel.CreateUnbounded <HubMessage>(); // Set the hub feature before doing anything else. This stores // all the relevant state for a SignalR Hub connection. connection.Features.Set <IHubFeature>(new HubFeature()); var connectionContext = new HubConnectionContext(output, connection); if (!await ProcessNegotiate(connectionContext)) { return; } connectionContext.UserIdentifier = _userIdProvider.GetUserId(connectionContext); // Hubs support multiple producers so we set up this loop to copy // data written to the HubConnectionContext's channel to the transport channel var protocolReaderWriter = connectionContext.ProtocolReaderWriter; async Task WriteToTransport() { try { while (await output.In.WaitToReadAsync()) { while (output.In.TryRead(out var hubMessage)) { var buffer = protocolReaderWriter.WriteMessage(hubMessage); while (await connection.Transport.Out.WaitToWriteAsync()) { if (connection.Transport.Out.TryWrite(buffer)) { break; } } } } } catch (Exception ex) { connectionContext.Abort(ex); } } var writingOutputTask = WriteToTransport(); try { await _lifetimeManager.OnConnectedAsync(connectionContext); await RunHubAsync(connectionContext); } finally { await _lifetimeManager.OnDisconnectedAsync(connectionContext); // Nothing should be writing to the HubConnectionContext output.Out.TryComplete(); // This should unwind once we complete the output await writingOutputTask; } }
public override void Abort() => _connection.Abort();