Exemple #1
0
        public void VMController_OnUpdateVM()
        {
            var vmController = new VMController(TestResponse, _vmFactory);

            vmController.OnRequestVM("conn1", typeof(UnitTestVM).Name);

            vmController.OnUpdateVM("conn1", typeof(UnitTestVM).Name, new Dictionary <string, object>()
            {
                { "LastName", "Doe" }, { "Age", 42 }
            });

            Assert.AreEqual("conn1", _connectionId);
            Assert.AreEqual(typeof(UnitTestVM).Name, _vmId);
            var vmData = (JObject)JsonConvert.DeserializeObject(_vmData);

            Assert.AreEqual("John Doe", vmData["FullName"]);

            vmController.OnRequestVM("conn1", typeof(UnitTestVM).Name);
            Assert.AreEqual("conn1", _connectionId);
            Assert.AreEqual(typeof(UnitTestVM).Name, _vmId);
            vmData = (JObject)JsonConvert.DeserializeObject(_vmData);
            Assert.AreEqual("Doe", vmData["LastName"]);
            Assert.AreEqual(42, vmData["Age"]);

            vmController.Dispose();
        }
Exemple #2
0
        public async Task <string> Request_VM(
            string vmId,
            [FromQuery] string vmArg,
            [FromServices] IVMFactory vmFactory,
            [FromServices] IHubServiceProvider hubServiceProvider,
            [FromServices] IVMServiceScopeFactory serviceScopeFactory,
            [FromServices] IHubPipeline hubPipeline,
            [FromServices] IPrincipalAccessor principalAccessor
            )
        {
            var taskCompletionSource = new TaskCompletionSource <string>(TaskCreationOptions.RunContinuationsAsynchronously);

            Task responseVM(string arg1, string arg2, string arg3)
            {
                taskCompletionSource.TrySetResult(arg3);
                return(Task.CompletedTask);
            }

            var vmController = new VMController(responseVM, vmFactory, serviceScopeFactory.CreateScope())
            {
                ResponseVMFilter = CreateRespondingVMFilter(hubPipeline, vmId, vmArg)
            };

            var httpCallerContext = InitializeContext(vmController, hubServiceProvider, principalAccessor);
            var connectionId      = httpCallerContext.ConnectionId;

            try
            {
                var hubContext = new DotNetifyHubContext(httpCallerContext, nameof(Request_VM), vmId, vmArg, BuildHeaders(), httpCallerContext.User);
                vmController.RequestVMFilter = CreateVMFilter(hubContext, hubPipeline);

                await hubPipeline.RunMiddlewaresAsync(hubContext, async ctx =>
                {
                    await vmController.OnRequestVMAsync(connectionId, ctx.VMId, ctx.Data);
                    vmController.Dispose();
                });
            }
            catch (Exception ex)
            {
                var finalEx = await hubPipeline.RunExceptionMiddlewareAsync(httpCallerContext, ex);

                if (finalEx is OperationCanceledException == false)
                {
                    taskCompletionSource.TrySetResult(DotNetifyHub.SerializeException(finalEx));
                }
            }

            return(await taskCompletionSource.Task);
        }
Exemple #3
0
        public async Task VMController_OnRequestVM()
        {
            var vmController = new VMController(TestResponse, _vmFactory);
            await vmController.OnRequestVMAsync("conn1", typeof(UnitTestVM).Name);

            Assert.AreEqual("conn1", _connectionId);
            Assert.AreEqual(typeof(UnitTestVM).Name, _vmId);
            var vmData = JsonConvert.DeserializeObject <UnitTestVM>(_vmData);

            Assert.IsNotNull(vmData);
            Assert.AreEqual("John", vmData.FirstName);
            Assert.AreEqual("Smith", vmData.LastName);
            Assert.AreEqual("John Smith", vmData.FullName);
            Assert.AreEqual(25, vmData.Age);

            vmController.Dispose();
        }
Exemple #4
0
        public async Task <string> Update_VM(
            string vmId,
            [FromQuery] string vmArg,
            [FromBody] Dictionary <string, object> vmData,
            [FromServices] IVMFactory vmFactory,
            [FromServices] IHubServiceProvider hubServiceProvider,
            [FromServices] IVMServiceScopeFactory serviceScopeFactory,
            [FromServices] IHubPipeline hubPipeline,
            [FromServices] IPrincipalAccessor principalAccessor)
        {
            var taskCompletionSource1 = new TaskCompletionSource <string>();
            var taskCompletionSource2 = new TaskCompletionSource <string>();

            var vmController = new VMController((arg1, arg2, arg3) =>
            {
                if (!taskCompletionSource1.TrySetResult(arg3))
                {
                    taskCompletionSource2.TrySetResult(arg3);
                }
            }, vmFactory, serviceScopeFactory.CreateScope())
            {
                ResponseVMFilter = CreateRespondingVMFilter(hubPipeline, vmId, vmData)
            };

            var httpCallerContext = InitializeContext(vmController, hubServiceProvider, principalAccessor);
            var connectionId      = httpCallerContext.ConnectionId;

            try
            {
                var hubContext = new DotNetifyHubContext(httpCallerContext, nameof(Request_VM), vmId, vmArg, BuildHeaders(), httpCallerContext.User);
                vmController.RequestVMFilter = CreateVMFilter(hubContext, hubPipeline);

                hubPipeline.RunMiddlewares(hubContext, ctx =>
                {
                    vmController.OnRequestVM(connectionId, ctx.VMId, ctx.Data);
                    return(Task.CompletedTask);
                });
            }
            catch (Exception ex)
            {
                var finalEx = hubPipeline.RunExceptionMiddleware(httpCallerContext, ex);
                if (finalEx is OperationCanceledException == false)
                {
                    taskCompletionSource1.TrySetResult(DotNetifyHub.SerializeException(finalEx));
                }
            }

            await taskCompletionSource1.Task;

            try
            {
                var hubContext = new DotNetifyHubContext(httpCallerContext, nameof(Update_VM), vmId, vmData, BuildHeaders(), httpCallerContext.User);
                vmController.UpdateVMFilter = CreateVMFilter(hubContext, hubPipeline);

                hubPipeline.RunMiddlewares(hubContext, ctx =>
                {
                    vmController.OnUpdateVM(connectionId, ctx.VMId, ctx.Data as Dictionary <string, object>);
                    vmController.Dispose();
                    return(Task.CompletedTask);
                });
            }
            catch (Exception ex)
            {
                var finalEx = hubPipeline.RunExceptionMiddleware(httpCallerContext, ex);
                if (finalEx is OperationCanceledException == false)
                {
                    taskCompletionSource2.TrySetResult(DotNetifyHub.SerializeException(finalEx));
                }
            }

            return(await taskCompletionSource2.Task);
        }