public async Task VMController_OnUpdateVM() { var vmController = new VMController(TestResponse, _vmFactory); await vmController.OnRequestVMAsync("conn1", typeof(UnitTestVM).Name); await vmController.OnUpdateVMAsync("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"]); await vmController.OnRequestVMAsync("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(); }
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>(TaskCreationOptions.RunContinuationsAsynchronously); var taskCompletionSource2 = new TaskCompletionSource <string>(TaskCreationOptions.RunContinuationsAsynchronously); Task responseVM(string arg1, string arg2, string arg3) { if (!taskCompletionSource1.TrySetResult(arg3)) { taskCompletionSource2.TrySetResult(arg3); } return(Task.CompletedTask); } var vmController = new VMController(responseVM, 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); await hubPipeline.RunMiddlewaresAsync(hubContext, async ctx => { await vmController.OnRequestVMAsync(connectionId, ctx.VMId, ctx.Data); }); } catch (Exception ex) { var finalEx = await hubPipeline.RunExceptionMiddlewareAsync(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); await hubPipeline.RunMiddlewaresAsync(hubContext, async ctx => { await vmController.OnUpdateVMAsync(connectionId, ctx.VMId, ctx.Data as Dictionary <string, object>); vmController.Dispose(); }); } catch (Exception ex) { var finalEx = await hubPipeline.RunExceptionMiddlewareAsync(httpCallerContext, ex); if (finalEx is OperationCanceledException == false) { taskCompletionSource2.TrySetResult(DotNetifyHub.SerializeException(finalEx)); } } return(await taskCompletionSource2.Task); }