/// <summary> /// This method is called by browser clients to remove its view model as it's no longer used. /// </summary> /// <param name="vmId">Identifies the view model. By convention, this should match a view model class name.</param> public void Dispose_VM(string vmId) { try { _callerContext = Context; _hubContext = new DotNetifyHubContext(_callerContext, nameof(Dispose_VM), vmId, null, null, Principal); _hubPipeline.RunMiddlewares(_hubContext, ctx => { Principal = ctx.Principal; VMController.OnDisposeVM(Context.ConnectionId, ctx.VMId); return(Task.CompletedTask); }); } catch (Exception ex) { _hubPipeline.RunExceptionMiddleware(Context, ex); } }
public async Task DisposeVMAsyc(string vmId) { try { _callerContext = Context; _hubContext = new DotNetifyHubContext(_callerContext, nameof(IDotNetifyHubMethod.Dispose_VM), vmId, null, null, Principal); await _hubPipeline.RunMiddlewaresAsync(_hubContext, ctx => { Principal = ctx.Principal; VMController.OnDisposeVM(Context.ConnectionId, ctx.VMId); return(Task.CompletedTask); }); } catch (Exception ex) { await _hubPipeline.RunExceptionMiddlewareAsync(Context, ex); } }
/// <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 JObject) { // Newtonsoft.Json protocol. data = vmArg as JObject; } else { // MessagePack protocol. data = JObject.FromObject(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)); } } }
public Task Invoke(DotNetifyHubContext hubContext, NextDelegate next) { string data = hubContext.Data == null ? string.Empty : hubContext.Data is string?(string)hubContext.Data : JsonConvert.SerializeObject(hubContext.Data, Formatting.None); var log = $@"[dotNetify] connId={hubContext.CallerContext?.ConnectionId} type={hubContext.CallType} vmId={hubContext.VMId} data={data.Replace("\r\n", "")}"; if (hubContext.Headers != null) { log += $@" headers={JsonConvert.SerializeObject(hubContext.Headers)}"; } _trace(log); return(next(hubContext)); }
public override Task Invoke(DotNetifyHubContext hubContext, NextDelegate next) { if (hubContext.CallType == nameof(DotNetifyHub.Request_VM)) { try { ValidateBearerToken(ParseHeaders <HeaderData>(hubContext.Headers), out SecurityToken validatedToken); if (validatedToken != null) { hubContext.PipelineData.Add("AccessToken", validatedToken); } } catch (Exception) { } } return(next(hubContext)); }
/// <summary> /// Runs the filter before the view model respond to something. /// </summary> private void RunRespondingVMFilters(string vmId, BaseVM vm, object vmData, Action <object> vmAction) { try { _hubContext = new DotNetifyHubContext(Context, nameof(Response_VM), vmId, vmData, null, Principal); _hubPipeline.RunMiddlewares(_hubContext, ctx => { Principal = ctx.Principal; RunVMFilters(vm, ctx.Data, vmAction); return(Task.CompletedTask); }); } catch (Exception ex) { var finalEx = _hubPipeline.RunExceptionMiddleware(Context, ex); if (finalEx is OperationCanceledException == false) { Response_VM(Context.ConnectionId, vmId, SerializeException(finalEx)); } } }
/// <summary> /// Runs the filter before the view model respond to something. /// </summary> private async Task RunRespondingVMFilters(string vmId, BaseVM vm, object vmData, VMController.VMActionDelegate vmAction) { try { _hubContext = new DotNetifyHubContext(_callerContext, nameof(IDotNetifyHubMethod.Response_VM), vmId, vmData, null, Principal); await _hubPipeline.RunMiddlewaresAsync(_hubContext, async ctx => { Principal = ctx.Principal; await RunVMFilters(vm, ctx.Data, vmAction); }); } catch (Exception ex) { var finalEx = await _hubPipeline.RunExceptionMiddlewareAsync(_callerContext, ex); if (finalEx is OperationCanceledException == false && _callerContext != null) { await ResponseVMAsync(_callerContext.ConnectionId, vmId, SerializeException(finalEx)); } } }
/// <summary> /// This method is called by browser clients to update a view model's value. /// </summary> /// <param name="vmId">Identifies the view model.</param> /// <param name="vmData">View model update data, where key is the property path and value is the property's new value.</param> public void Update_VM(string vmId, Dictionary <string, object> vmData) { try { _hubContext = new DotNetifyHubContext(Context, nameof(Request_VM), vmId, vmData, null, Principal); _hubPipeline.RunMiddlewares(_hubContext, ctx => { Principal = ctx.Principal; VMController.OnUpdateVM(ctx.CallerContext.ConnectionId, ctx.VMId, ctx.Data as Dictionary <string, object>); 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)); } } }
/// <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)); } } }
public async Task UpdateVMAsync(string vmId, Dictionary <string, object> vmData) { var data = vmData?.ToDictionary(x => x.Key, x => NormalizeType(x.Value)); try { _callerContext = Context; _hubContext = new DotNetifyHubContext(_callerContext, nameof(IDotNetifyHubMethod.Update_VM), vmId, data, null, Principal); await _hubPipeline.RunMiddlewaresAsync(_hubContext, async ctx => { Principal = ctx.Principal; await VMController.OnUpdateVMAsync(ctx.CallerContext.ConnectionId, ctx.VMId, ctx.Data as Dictionary <string, object>); }); } catch (Exception ex) { var finalEx = await _hubPipeline.RunExceptionMiddlewareAsync(Context, ex); if (finalEx is OperationCanceledException == false) { await ResponseVMAsync(Context.ConnectionId, vmId, SerializeException(finalEx)); } } }
/// <summary> /// This method is called by browser clients to update a view model's value. /// </summary> /// <param name="vmId">Identifies the view model.</param> /// <param name="vmData">View model update data, where key is the property path and value is the property's new value.</param> public void Update_VM(string vmId, Dictionary <string, object> vmData) { var data = vmData?.ToDictionary(x => x.Key, x => NormalizeType(x.Value)); try { _callerContext = Context; _hubContext = new DotNetifyHubContext(_callerContext, nameof(Update_VM), vmId, data, null, Principal); _hubPipeline.RunMiddlewares(_hubContext, ctx => { Principal = ctx.Principal; VMController.OnUpdateVM(ctx.CallerContext.ConnectionId, ctx.VMId, ctx.Data as Dictionary <string, object>); return(Task.CompletedTask); }); } catch (Exception ex) { var finalEx = _hubPipeline.RunExceptionMiddleware(Context, ex); if (finalEx is OperationCanceledException == false) { Response_VM(Context.ConnectionId, vmId, SerializeException(finalEx)); } } }
public void RunMiddlewares(DotNetifyHubContext hubContext, NextDelegate finalAction) { _ = RunMiddlewaresAsync(hubContext, finalAction); }
public void RunVMFilters(DotNetifyHubContext hubContext, BaseVM vm, NextFilterDelegate finalFilter) { _ = RunVMFiltersAsync(hubContext, vm, finalFilter); }
/// <summary> /// Handles responses back to the hub forwarder by running it through the middleware pipeline. /// </summary> /// <param name="vmId">Identifies the view model.</param> /// <param name="vmData">View model response data.</param> private async Task OnHubForwardResponseAsync(string vmId, object vmData) { var hubContext = new DotNetifyHubContext(Context, nameof(IDotNetifyHubMethod.Response_VM), vmId, vmData, null, Context.User); await _hubPipeline.RunMiddlewaresAsync(hubContext, ctx => Task.CompletedTask); }