Ejemplo n.º 1
0
        /// <summary>
        /// This method is called by browser clients to request view model data.
        /// </summary>
        /// <param name="vmId">Identifies the view model.</param>
        /// <param name="vmArg">Optional argument that may contain view model's initialization argument and/or request headers.</param>
        public void Request_VM(string vmId, object vmArg)
        {
            object data = NormalizeType(vmArg);

            try
            {
                _hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, data, null, Principal);
                _hubPipeline.RunMiddlewares(_hubContext, ctx =>
                {
                    Principal        = ctx.Principal;
                    string groupName = VMController.OnRequestVM(Context.ConnectionId, ctx.VMId, ctx.Data);

                    // A multicast view model may be assigned to a SignalR group. If so, add the connection to the group.
                    if (!string.IsNullOrEmpty(groupName))
                    {
                        Groups.Add(Context.ConnectionId, groupName);
                    }

                    return(Task.FromResult(0));
                });
            }
            catch (Exception ex)
            {
                var finalEx = _hubPipeline.RunExceptionMiddleware(Context, ex);
                if (finalEx is OperationCanceledException == false)
                {
                    Response_VM(Context.ConnectionId, vmId, SerializeException(finalEx));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method is called by browser clients to request view model data.
        /// </summary>
        /// <param name="vmId">Identifies the view model.</param>
        /// <param name="vmArg">Optional argument that may contain view model's initialization argument and/or request headers.</param>
        public void Request_VM(string vmId, object vmArg)
        {
            JObject data = null;

            if (vmArg != null)
            {
                if (vmArg is JsonElement)
                {
                    // System.Text.Json protocol.
                    data = JObject.Parse(((JsonElement)vmArg).GetRawText());
                }
                else if (vmArg is JObject)
                {
                    // Newtonsoft.Json protocol.
                    data = vmArg as JObject;
                }
                else
                {
                    // MessagePack protocol.
                    data = JObject.FromObject(vmArg);
                }
            }

            try
            {
                _callerContext = Context;
                _hubContext    = new DotNetifyHubContext(_callerContext, nameof(Request_VM), vmId, data, null, Principal);
                _hubPipeline.RunMiddlewares(_hubContext, ctx =>
                {
                    Principal        = ctx.Principal;
                    string groupName = VMController.OnRequestVM(Context.ConnectionId, ctx.VMId, ctx.Data);

                    // A multicast view model may be assigned to a SignalR group. If so, add the connection to the group.
                    if (!string.IsNullOrEmpty(groupName))
                    {
                        _globalHubContext.Groups.AddToGroupAsync(Context.ConnectionId, groupName);
                    }

                    return(Task.CompletedTask);
                });
            }
            catch (Exception ex)
            {
                var finalEx = _hubPipeline.RunExceptionMiddleware(Context, ex);
                if (finalEx is OperationCanceledException == false)
                {
                    Response_VM(Context.ConnectionId, vmId, SerializeException(finalEx));
                }
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// This method is called by the VMManager to send response back to browser clients.
 /// </summary>
 /// <param name="connectionId">Identifies the browser client making prior request.</param>
 /// <param name="vmId">Identifies the view model.</param>
 /// <param name="vmData">View model data in serialized JSON.</param>
 public void SendResponse(string connectionId, string vmId, string vmData)
 {
     try
     {
         _hubPipeline.RunMiddlewares(Context, nameof(Response_VM), vmId, vmData, ctx =>
         {
             Response_VM(connectionId, ctx.VMId, ctx.Data as string);
             return(Task.CompletedTask);
         });
     }
     catch (Exception ex)
     {
         _hubPipeline.RunExceptionMiddleware(Context, ex);
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// This method is called by browser clients to request view model data.
 /// </summary>
 /// <param name="vmId">Identifies the view model.</param>
 /// <param name="vmArg">Optional argument that may contain view model's initialization argument and/or request headers.</param>
 public void Request_VM(string vmId, object vmArg)
 {
     try
     {
         _hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, vmArg, null, Principal);
         _hubPipeline.RunMiddlewares(_hubContext, ctx =>
         {
             Principal = ctx.Principal;
             VMController.OnRequestVM(Context.ConnectionId, ctx.VMId, ctx.Data);
             return(Task.CompletedTask);
         });
     }
     catch (Exception ex)
     {
         var finalEx = _hubPipeline.RunExceptionMiddleware(Context, ex);
         if (finalEx is OperationCanceledException == false)
         {
             Response_VM(Context.ConnectionId, vmId, SerializeException(finalEx));
         }
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Creates view model filter delegate that runs before it responds to something.
 /// </summary>
 /// <param name="hubContext">DotNetify hub context.</param>
 /// <param name="hubPipeline">Middleware/VM filter pipeline.</param>
 /// <param name="vmData">View model data from the request.</param>
 /// <returns>View model filter delegate.</returns>
 private VMController.FilterDelegate CreateRespondingVMFilter(IHubPipeline hubPipeline, string vmId, object vmData)
 {
     return((_vmId, vm, data, vmAction) =>
     {
         var hubContext = new DotNetifyHubContext(new HttpCallerContext(HttpContext), nameof(DotNetifyHub.Response_VM), vmId, vmData, BuildHeaders(), HttpContext?.User);
         hubPipeline.RunMiddlewares(hubContext, ctx =>
         {
             CreateVMFilter(hubContext, hubPipeline)(_vmId, vm, data, vmAction);
             return Task.CompletedTask;
         });
     });
 }
Ejemplo n.º 6
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>();
            var vmController         = new VMController((arg1, arg2, arg3) => taskCompletionSource.TrySetResult(arg3), 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);

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

            return(await taskCompletionSource.Task);
        }