public Task<PushNotification> GetPushNotification() { SecretUserId userId = UserIdExtractor.ReadSecretUserIdFromRequest(this.Request); User user; using (TransmitContext context = new TransmitContext()) { UserIdValidator validator = new UserIdValidator(context); validator.RequireSignedUserIdValid(userId); user = PushNotificationManager.GetUserById(userId.UserId); if (user == null) { UserManager userManager = new UserManager(context); user = userManager.GetUserByUserIdOrThrow(userId.UserId); } } TaskCompletionSource<PushNotification> completionSource = new TaskCompletionSource<PushNotification>(); #if DEBUG int id = _pushConnectionId++; Trace.TraceInformation("Notification {0} was started", id); #endif PushConnection pushConnection = new PushConnection(HttpContext.Current); pushConnection.SendPushNotificationEvent += notification => { #if DEBUG Trace.TraceInformation("Sending notification to client:" + id); #endif completionSource.SetResult(notification); }; user.PushConnections.Add(pushConnection); PushNotificationManager.UpdateOrAddUserToNotifiactionQuery(user); return completionSource.Task; }