Example #1
0
        public void AddTaskTest()
        {
            manager.AddConnection(connection.Object, testStr);

            Assert.Throws <KeyNotFoundException>(() => manager.TaskList[testStr]);
            manager.AddTask(testStr, new CancellationTokenSource());

            Assert.NotEmpty(manager.TaskList[testStr]);
        }
Example #2
0
        /// <summary>
        /// Creates the task that will be invoked after request is processed in filter.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="func">The function.</param>
        /// <returns></returns>
        public string CreateTask <T>(Func <T, CancellationToken, string, Task> func) where T : WebSocketService
        {
            if (func == null)
            {
                throw new ArgumentNullException(nameof(func));
            }
            var tokenSource = new CancellationTokenSource();

            var connection = WebSocketManager.GetConnetionById(ConnectionId);

            connection.ViewModelState.LastSentViewModel = Context.ViewModel;

            var taskId = WebSocketManager.AddTask(
                ConnectionId,
                tokenSource
                );

            var task = WebSocketManager.TaskList.SelectMany(v => v.Value).FirstOrDefault(t => t.TaskId == taskId);

            if (task != null)
            {
                task.FunctionToInvoke = () =>
                                        func.Invoke((T)this, tokenSource.Token, taskId).ContinueWith(s => StopTask(taskId), tokenSource.Token);
            }

            return(taskId);
        }