/// <summary> /// Creates a dotNetify hub that uses HTTP context for its hub context, and sets the response callback to a local function. /// </summary> /// <param name="vmControllerFactory">Factory of view model controllers.</param> /// <param name="serviceProvider">Allows to provide scoped service provider for the view models.</param> /// <param name="principalAccessor">Allows to pass the hub principal.</param> /// <param name="hubPipeline">Manages middlewares and view model filters.</param> /// <param name="hubResponseFactory">Factory of objects to send responses back to hub clients.</param> /// <param name="callType">Hub call type.</param> /// <param name="vmId">Identifies the view model.</param> /// <param name="data">View model data.</param> /// <returns>Hub instance.</returns> private IDotNetifyHubHandler CreateHubHandler( IVMControllerFactory vmControllerFactory, IHubServiceProvider hubServiceProvider, IPrincipalAccessor principalAccessor, IHubPipeline hubPipeline, IDotNetifyHubResponseManager hubResponseManager, string callType, string vmId, object data = null) { var httpCallerContext = new HttpCallerContext(HttpContext); if (principalAccessor is HubInfoAccessor) { var hubPrincipalAccessor = principalAccessor as HubInfoAccessor; hubPrincipalAccessor.Principal = HttpContext?.User; hubPrincipalAccessor.Context = new DotNetifyHubContext(httpCallerContext, callType, vmId, data, null, hubPrincipalAccessor.Principal); } return(new DotNetifyHubHandler(vmControllerFactory, hubServiceProvider, principalAccessor, hubPipeline, hubResponseManager) { CallerContext = httpCallerContext, OnVMResponse = ResponseVMCallback }); }
/// <summary> /// Constructor for dependency injection. /// </summary> /// <param name="vmControllerFactory">Factory of view model controllers.</param> /// <param name="principalAccessor">Allow to pass the hub principal.</param> /// <param name="hubPipeline">Manages middlewares and view model filters.</param> public DotNetifyHub(IVMControllerFactory vmControllerFactory, IPrincipalAccessor principalAccessor, IHubPipeline hubPipeline) { _vmControllerFactory = vmControllerFactory; _vmControllerFactory.ResponseDelegate = Response_VM; _principalAccessor = principalAccessor; _hubPipeline = hubPipeline; }
public async Task <string> Request_VM( string vmId, [FromQuery] string vmArg, [FromServices] WebApiVMControllerFactory vmControllerFactory, [FromServices] IHubServiceProvider hubServiceProvider, [FromServices] IPrincipalAccessor principalAccessor, [FromServices] IHubPipeline hubPipeline, [FromServices] IDotNetifyHubResponseManager hubResponseManager ) { var hub = CreateHubHandler(vmControllerFactory, hubServiceProvider, principalAccessor, hubPipeline, hubResponseManager, nameof(IDotNetifyHubMethod.Request_VM), vmId, vmArg); try { await hub.RequestVMAsync(vmId, vmArg); _taskCompletionSource.TrySetResult(_responses.LastOrDefault()); _responses.Clear(); } catch (Exception ex) { _taskCompletionSource.TrySetResult(ex.Serialize()); } return(await _taskCompletionSource.Task); }
public static void ConfigureSignalR(IDependencyResolver dependencyResolver, IHubPipeline hubPipeline) { // Uncomment the following line to enable scale-out using SQL Server //dependencyResolver.UseSqlServer(ConfigurationManager.ConnectionStrings["SignalRSamples"].ConnectionString); hubPipeline.AddModule(new SamplePipelineModule()); hubPipeline.EnableAutoRejoiningGroups(); }
/// <summary> /// Constructor for dependency injection. /// </summary> /// <param name="vmControllerFactory">Factory of view model controllers.</param> /// <param name="principalAccessor">Allow to pass the hub principal.</param> /// <param name="hubPipeline">Manages middlewares and view model filters.</param> /// <param name="globalHubContext">Provides access to hubs.</param> public DotNetifyHub(IVMControllerFactory vmControllerFactory, IPrincipalAccessor principalAccessor, IHubPipeline hubPipeline, IHubContext <DotNetifyHub> globalHubContext) { _vmControllerFactory = vmControllerFactory; _vmControllerFactory.ResponseDelegate = Response_VM; _principalAccessor = principalAccessor; _hubPipeline = hubPipeline; _globalHubContext = globalHubContext; }
/// <summary> /// By default, clients that are reconnecting to the server will be removed from all groups they may have previously been a member of. /// Enabling AutoRejoiningGroups allows all clients to rejoin the all of the hub groups the claim to be a member of automatically /// on connect and reconnect. /// Enabling this module may be insecure because untrusted clients may claim to be a member of groups they were never authorized to join. /// </summary> /// <param name="pipeline">The <see cref="IHubPipeline" /> to which the <see cref="AutoRejoiningGroupsModule" /> will be added.</param> public static void EnableAutoRejoiningGroups(this IHubPipeline pipeline) { if (pipeline == null) { throw new ArgumentNullException("pipeline"); } pipeline.AddModule(new AutoRejoiningGroupsModule()); }
public static void ConfigureSignalR(IDependencyResolver dependencyResolver, IHubPipeline hubPipeline) { // Uncomment the following line to enable scale-out using SQL Server // dependencyResolver.UseSqlServer(ConfigurationManager.ConnectionStrings["SignalRSamples"].ConnectionString); // Uncomment the following line to enable scale-out using Redis // dependencyResolver.UseRedis("127.0.0.1", 6379, "", new[] { "SignalRSamples" }); hubPipeline.AddModule(new SamplePipelineModule()); }
/// <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(async(_vmId, vm, data, vmAction) => { var hubContext = new DotNetifyHubContext(new HttpCallerContext(HttpContext), nameof(DotNetifyHub.ResponseVMAsync), vmId, vmData, BuildHeaders(), HttpContext?.User); await hubPipeline.RunMiddlewaresAsync(hubContext, async ctx => { await CreateVMFilter(hubContext, hubPipeline)(_vmId, vm, data, vmAction); }); }); }
/// <summary> /// Requiring Authentication adds an <see cref="AuthorizeModule"/> to the <see cref="IHubPipeline" /> with <see cref="IAuthorizeHubConnection"/> /// and <see cref="IAuthorizeHubMethodInvocation"/> authorizers that will be applied globally to all hubs and hub methods. /// These authorizers require that the <see cref="System.Security.Principal.IPrincipal"/>'s <see cref="System.Security.Principal.IIdentity"/> /// IsAuthenticated for any clients that invoke server-side hub methods or receive client-side hub method invocations. /// </summary> /// <param name="pipeline">The <see cref="IHubPipeline" /> to which the <see cref="AuthorizeModule" /> will be added.</param> public static void RequireAuthentication(this IHubPipeline pipeline) { if (pipeline == null) { throw new ArgumentNullException("pipeline"); } var authorizer = new AuthorizeAttribute(); pipeline.AddModule(new AuthorizeModule(globalConnectionAuthorizer: authorizer, globalInvocationAuthorizer: authorizer)); }
/// <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; }); }); }
public async Task Dispose_VM( string vmId, [FromServices] WebApiVMControllerFactory vmControllerFactory, [FromServices] IHubServiceProvider hubServiceProvider, [FromServices] IPrincipalAccessor principalAccessor, [FromServices] IHubPipeline hubPipeline, [FromServices] IDotNetifyHubResponseManager hubResponseManager ) { var hub = CreateHubHandler(vmControllerFactory, hubServiceProvider, principalAccessor, hubPipeline, hubResponseManager, nameof(IDotNetifyHubMethod.Dispose_VM), vmId); await hub.DisposeVMAsync(vmId); }
/// <summary> /// Constructor for dependency injection. /// </summary> /// <param name="vmControllerFactory">Factory of view model controllers.</param> /// <param name="serviceProvider">Allows to provide scoped service provider for the view models.</param> /// <param name="principalAccessor">Allows to pass the hub principal.</param> /// <param name="hubPipeline">Manages middlewares and view model filters.</param> /// <param name="globalHubContext">Provides access to hubs.</param> public DotNetifyHub( IVMControllerFactory vmControllerFactory, IHubServiceProvider serviceProvider, IPrincipalAccessor principalAccessor, IHubPipeline hubPipeline, IHubContext <DotNetifyHub> globalHubContext) { _vmControllerFactory = vmControllerFactory; _vmControllerFactory.ResponseDelegate = ResponseVMAsync; _serviceProvider = serviceProvider; _principalAccessor = principalAccessor; _hubPipeline = hubPipeline; _globalHubContext = globalHubContext; }
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); }
private void Configure(IOwinConfiguration owin) { if (owin == null) { throw new ArgumentNullException(nameof(owin)); } owin.Http.Routes.IgnoreRoute("signalR", "signalr/{*pathInfo}"); IDependencyResolver resolver = GlobalHost.DependencyResolver = new CustomResolver(GlobalHost.DependencyResolver, owin.Kernel); var hubConfiguration = new HubConfiguration { Resolver = resolver, EnableDetailedErrors = _enableDetailedErrors, EnableJavaScriptProxies = _enableJavaScriptProxies, EnableJSONP = _enableJSONP }; resolver.Register(typeof(IAssemblyLocator), () => new CustomAssemblyLocator(_assemblies)); resolver.Register(typeof(IHubActivator), () => new CustomHubActivator(resolver)); resolver.Register(typeof(IHubDescriptorProvider), () => new CustomHubDescriptorProvider(resolver, owin.Kernel.Resolve <IHubsProvider>())); IHubPipeline hubPipeline = resolver.Resolve <IHubPipeline>(); foreach (var pipelineModule in owin.Kernel.ResolveAll <IHubPipelineModule>()) { hubPipeline.AddModule(pipelineModule); } owin.App.Map(_path, subApp => { _onMap?.Invoke(subApp); subApp.RunSignalR(hubConfiguration); }); // TODO: Look at the possibility to add custom trace sources programatically // https://msdn.microsoft.com/en-us/library/ms228984(v=vs.110).aspx if (!_skipTraceConfiguration) { ITraceManager traceManager = resolver.Resolve <ITraceManager>(); traceManager.Switch.Level = _traceLevel; } }
public static void ConfigureSignalR(IDependencyResolver dependencyResolver, IHubPipeline hubPipeline) { // Uncomment the following line to enable scale-out using SQL Server //dependencyResolver.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings["SignalRSamples"].ConnectionString); // Uncomment the following line to enable scale-out using Redis //var config = new RedisScaleoutConfiguration("127.0.0.1", 6379, "", "SignalRSamples"); //config.RetryOnError = true; //dependencyResolver.UseRedis(config); //dependencyResolver.UseRedis("127.0.0.1", 6379, "", "SignalRSamples"); // Uncomment the following line to enable scale-out using service bus //dependencyResolver.UseServiceBus("connection string", "Microsoft.AspNet.SignalR.Samples"); hubPipeline.AddModule(new SamplePipelineModule()); }
public static void ConfigureSignalR(IDependencyResolver dependencyResolver, IHubPipeline hubPipeline) { // Uncomment the following line to enable scale-out using SQL Server //dependencyResolver.UseSqlServer(System.Configuration.ConfigurationManager.ConnectionStrings["SignalRSamples"].ConnectionString); // Uncomment the following line to enable scale-out using Redis //var redisConnection = ConfigurationManager.AppSettings["redis:connectionString"]; //if (!string.IsNullOrEmpty(redisConnection)) //{ // dependencyResolver.UseRedis(new RedisScaleoutConfiguration(redisConnection, "SignalRSamples")); //} // Uncomment the following line to enable scale-out using service bus //dependencyResolver.UseServiceBus("connection string", "Microsoft.AspNet.SignalR.Samples"); hubPipeline.AddModule(new SamplePipelineModule()); }
public static void ConfigureSignalR(IDependencyResolver dependencyResolver, IHubPipeline hubPipeline) { // Uncomment the following line to enable scale-out using SQL Server //SignalRScaleOutDatabase(ref dependencyResolver); string connection = string.Empty; try { ClubCloudServiceClient client = new ClubCloudServiceClient(SPServiceContext.Current); connection = client.ScaleOutConnection("ClubCloudService_SignalR"); } catch { } finally { if (!string.IsNullOrWhiteSpace(connection)) { dependencyResolver.UseSqlServer(connection); } } // Any connection or hub wire up and configuration should go here hubPipeline.AddModule(new ClubCloudPipelineModule()); }
public static void RequireAuthentication(this IHubPipeline pipeline) { var authorizer = new AuthorizeAttribute(); pipeline.AddModule(new AuthorizeModule(globalConnectionAuthorizer: authorizer, globalInvocationAuthorizer: authorizer)); }
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); }
public static void EnableAutoRejoiningGroups(this IHubPipeline pipeline) { pipeline.AddModule(new AutoRejoiningGroupsModule()); }
/// <summary> /// Creates view model filter delegate that runs before it is requested/updated. /// </summary> /// <param name="hubContext">DotNetify hub context.</param> /// <param name="hubPipeline">Middleware/VM filter pipeline.</param> /// <returns>View model filter delegate.</returns> private VMController.FilterDelegate CreateVMFilter(DotNetifyHubContext hubContext, IHubPipeline hubPipeline) { return((vmId, vm, data, vmAction) => { try { hubContext.Data = data; hubPipeline.RunVMFilters(hubContext, vm, ctx => { vmAction(ctx.HubContext.Data); return Task.CompletedTask; }); } catch (TargetInvocationException ex) { throw ex.InnerException; } }); }
/// <summary> /// Constructor for dependency injection. /// </summary> /// <param name="hubHandler">Handles hub method invocation.</param> /// <param name="hubPipeline">Middleware pipeline.</param> public DotNetifyHub(IDotNetifyHubHandler hubHandler, IHubPipeline hubPipeline) { _hubHandler = hubHandler; _hubPipeline = hubPipeline; }
/// <summary> /// Adds an Audit Module to the hub pipeline. This must be called the before any methods on the Microsoft.AspNet.SignalR.Hubs.IHubPipelineInvoker are invoked. /// </summary> /// <param name="pipeline">The hub pipeline</param> /// <param name="config">The audit module configuration</param> /// <returns></returns> public static IHubPipeline AddAuditModule(this IHubPipeline pipeline, Action <IPipelineBuilder> config) { GlobalHost.HubPipeline.AddModule(AuditPipelineModule.Create(config)); return(pipeline); }
/// <summary> /// Creates view model filter delegate that runs before it is requested/updated. /// </summary> /// <param name="hubContext">DotNetify hub context.</param> /// <param name="hubPipeline">Middleware/VM filter pipeline.</param> /// <returns>View model filter delegate.</returns> private VMController.FilterDelegate CreateVMFilter(DotNetifyHubContext hubContext, IHubPipeline hubPipeline) { return(async(vmId, vm, data, vmAction) => { try { hubContext.Data = data; await hubPipeline.RunVMFiltersAsync(hubContext, vm, async ctx => { await vmAction(ctx.HubContext.Data); }); } catch (TargetInvocationException ex) { throw ex.InnerException; } }); }
public static void ConfigureSignalR(IDependencyResolver dependencyResolver, IHubPipeline hubPipeline) { hubPipeline.AddModule(new SamplePipelineModule()); }