private static void PreserveCultureUnsafeOnCompleted(ICriticalNotifyCompletion notifier,
                                                             Action continuation,
                                                             bool useSyncContext)
        {
#if NETSTANDARD1_3 || NETSTANDARD1_5
            notifier.UnsafeOnCompleted(continuation);
#else
            // Rely on the SyncContext to preserve culture if it exists
            if (useSyncContext && SynchronizationContext.Current != null)
            {
                notifier.UnsafeOnCompleted(continuation);
            }
            else
            {
                var preservedCulture = TaskAsyncHelper.SaveCulture();
                notifier.UnsafeOnCompleted(() =>
                {
                    TaskAsyncHelper.RunWithPreservedCulture(preservedCulture, continuation);
                });
            }
#endif
        }
        /// <summary>
        /// Handles all requests for <see cref="PersistentConnection"/>s.
        /// </summary>
        /// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
        /// <returns>A <see cref="Task"/> that completes when the <see cref="PersistentConnection"/> pipeline is complete.</returns>
        /// <exception cref="T:System.InvalidOperationException">
        /// Thrown if the transport wasn't specified.
        /// Thrown if the connection id wasn't specified.
        /// </exception>
        public virtual async Task ProcessRequestCore(HttpContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            if (IsNegotiationRequest(context.Request))
            {
                await ProcessNegotiationRequest(context).PreserveCulture();

                return;
            }
            else if (IsPingRequest(context.Request))
            {
                await ProcessPingRequest(context).PreserveCulture();

                return;
            }

            Transport = GetTransport(context);

            if (Transport == null)
            {
                await FailResponse(context.Response, String.Format(CultureInfo.CurrentCulture, Resources.Error_ProtocolErrorUnknownTransport)).PreserveCulture();

                return;
            }

            string connectionToken = context.Request.Query["connectionToken"];

            // If there's no connection id then this is a bad request
            if (String.IsNullOrEmpty(connectionToken))
            {
                await FailResponse(context.Response, String.Format(CultureInfo.CurrentCulture, Resources.Error_ProtocolErrorMissingConnectionToken)).PreserveCulture();

                return;
            }

            string connectionId;
            string message;
            int    statusCode;

            if (!TryGetConnectionId(context, connectionToken, out connectionId, out message, out statusCode))
            {
                await FailResponse(context.Response, message, statusCode).PreserveCulture();

                return;
            }

            // Set the transport's connection id to the unprotected one
            Transport.ConnectionId = connectionId;

            // Get the user id from the request
            string userId = UserIdProvider.GetUserId(context.Request);

            // Get the groups token from the request
            string groupsToken = await Transport.GetGroupsToken().PreserveCulture();

            IList <string> signals = GetSignals(userId, connectionId);
            IList <string> groups  = AppendGroupPrefixes(context, connectionId, groupsToken);

            Connection connection = CreateConnection(connectionId, signals, groups);

            Connection = connection;
            string groupName = PrefixHelper.GetPersistentConnectionGroupName(DefaultSignalRaw);

            Groups = new GroupManager(connection, groupName);

            // We handle /start requests after the PersistentConnection has been initialized,
            // because ProcessStartRequest calls OnConnected.
            if (IsStartRequest(context.Request))
            {
                await ProcessStartRequest(context, connectionId).PreserveCulture();

                return;
            }

            Transport.Connected = () =>
            {
                return(TaskAsyncHelper.FromMethod(() => OnConnected(context.Request, connectionId).OrEmpty()));
            };

            Transport.Reconnected = () =>
            {
                return(TaskAsyncHelper.FromMethod(() => OnReconnected(context.Request, connectionId).OrEmpty()));
            };

            Transport.Received = data =>
            {
                Counters.ConnectionMessagesSentTotal.Increment();
                Counters.ConnectionMessagesSentPerSec.Increment();
                return(TaskAsyncHelper.FromMethod(() => OnReceived(context.Request, connectionId, data).OrEmpty()));
            };

            Transport.Disconnected = clean =>
            {
                return(TaskAsyncHelper.FromMethod(() => OnDisconnected(context.Request, connectionId, stopCalled: clean).OrEmpty()));
            };

            await Transport.ProcessRequest(connection).OrEmpty().Catch(Logger, Counters.ErrorsAllTotal, Counters.ErrorsAllPerSec).PreserveCulture();
        }