Beispiel #1
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);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a view model controller and assigns it a key.
        /// On subsequent calls, use the same key to return the same object.
        /// </summary>
        /// <param name="key">Identifies the object.</param>
        /// <returns>View model controller.</returns>
        public VMController GetInstance(string key)
        {
            var cache = _controllersCache;

            if (!cache.TryGetValue(key, out Lazy <VMController> cachedValue))
            {
                cachedValue = new Lazy <VMController>(() => new VMController(ResponseDelegate, _serviceScopeFactory.CreateScope()));
                cache.Set(key, cachedValue, GetCacheEntryOptions());
            }
            return(cachedValue?.Value);
        }
Beispiel #3
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);
        }