private async Task <bool> EnsureConnectionStateAsync(DefaultConnectionContext connection, HttpContext context, TransportType transportType, TransportType supportedTransports, ConnectionLogScope logScope, HttpSocketOptions options) { if ((supportedTransports & transportType) == 0) { context.Response.ContentType = "text/plain"; context.Response.StatusCode = StatusCodes.Status404NotFound; _logger.TransportNotSupported(transportType); await context.Response.WriteAsync($"{transportType} transport not supported by this end point type"); return(false); } // Set the IHttpConnectionFeature now that we can access it. connection.Features.Set(context.Features.Get <IHttpConnectionFeature>()); var transport = (TransportType?)connection.Metadata[ConnectionMetadataNames.Transport]; if (transport == null) { connection.Metadata[ConnectionMetadataNames.Transport] = transportType; } else if (transport != transportType) { context.Response.ContentType = "text/plain"; context.Response.StatusCode = StatusCodes.Status400BadRequest; _logger.CannotChangeTransport(transport.Value, transportType); await context.Response.WriteAsync("Cannot change transports mid-connection"); return(false); } // Configure transport-specific features. if (transportType == TransportType.LongPolling) { connection.Features.Set <IConnectionInherentKeepAliveFeature>(new ConnectionInherentKeepAliveFeature(options.LongPolling.PollTimeout)); } // Setup the connection state from the http context connection.User = context.User; connection.SetHttpContext(context); // this is the default setting which should be overwritten by transports that have different capabilities (e.g. SSE) connection.TransportCapabilities = TransferMode.Binary | TransferMode.Text; // Set the Connection ID on the logging scope so that logs from now on will have the // Connection ID metadata set. logScope.ConnectionId = connection.ConnectionId; return(true); }
private async Task <bool> EnsureConnectionStateAsync(DefaultConnectionContext connection, HttpContext context, TransportType transportType, TransportType supportedTransports, ConnectionLogScope logScope) { if ((supportedTransports & transportType) == 0) { context.Response.StatusCode = StatusCodes.Status404NotFound; _logger.TransportNotSupported(connection.ConnectionId, transportType); await context.Response.WriteAsync($"{transportType} transport not supported by this end point type"); return(false); } var transport = (TransportType?)connection.Metadata[ConnectionMetadataNames.Transport]; if (transport == null) { connection.Metadata[ConnectionMetadataNames.Transport] = transportType; } else if (transport != transportType) { context.Response.StatusCode = StatusCodes.Status400BadRequest; _logger.CannotChangeTransport(connection.ConnectionId, transport.Value, transportType); await context.Response.WriteAsync("Cannot change transports mid-connection"); return(false); } // Setup the connection state from the http context connection.User = context.User; connection.SetHttpContext(context); // this is the default setting which should be overwritten by transports that have different capabilities (e.g. SSE) connection.TransportCapabilities = TransferMode.Binary | TransferMode.Text; // Set the Connection ID on the logging scope so that logs from now on will have the // Connection ID metadata set. logScope.ConnectionId = connection.ConnectionId; return(true); }