From 3c2e16bf92bf585be7597eb47d7f5d6283f7a558 Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Fri, 31 May 2019 22:25:08 +0200 Subject: [PATCH] Exposes the underlying HttpConnectionDispatcherOptions in MapBlazorHub (#10690) * Expose new overloads to customize connection dispatch options --- ...NetCore.Components.Server.netcoreapp3.0.cs | 5 + ...ComponentEndpointRouteBuilderExtensions.cs | 205 ++++++++++++++++-- ...onentEndpointRouteBuilderExtensionsTest.cs | 64 ++++++ 3 files changed, 260 insertions(+), 14 deletions(-) create mode 100644 src/Components/Server/test/ComponentEndpointRouteBuilderExtensionsTest.cs diff --git a/src/Components/Server/ref/Microsoft.AspNetCore.Components.Server.netcoreapp3.0.cs b/src/Components/Server/ref/Microsoft.AspNetCore.Components.Server.netcoreapp3.0.cs index f0254238ba..91d7621b0f 100644 --- a/src/Components/Server/ref/Microsoft.AspNetCore.Components.Server.netcoreapp3.0.cs +++ b/src/Components/Server/ref/Microsoft.AspNetCore.Components.Server.netcoreapp3.0.cs @@ -10,10 +10,15 @@ namespace Microsoft.AspNetCore.Builder public static partial class ComponentEndpointRouteBuilderExtensions { public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { throw null; } + public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Action configureOptions) { throw null; } public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Type type, string selector) { throw null; } + public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Type type, string selector, System.Action configureOptions) { throw null; } public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Type componentType, string selector, string path) { throw null; } + public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Type componentType, string selector, string path, System.Action configureOptions) { throw null; } public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; } + public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector, System.Action configureOptions) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; } public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector, string path) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; } + public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector, string path, System.Action configureOptions) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; } } } namespace Microsoft.AspNetCore.Components.Browser.Rendering diff --git a/src/Components/Server/src/Builder/ComponentEndpointRouteBuilderExtensions.cs b/src/Components/Server/src/Builder/ComponentEndpointRouteBuilderExtensions.cs index 5a65519916..4248dae1d7 100644 --- a/src/Components/Server/src/Builder/ComponentEndpointRouteBuilderExtensions.cs +++ b/src/Components/Server/src/Builder/ComponentEndpointRouteBuilderExtensions.cs @@ -4,7 +4,9 @@ using System; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Server; +using Microsoft.AspNetCore.Http.Connections; using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.SignalR; namespace Microsoft.AspNetCore.Builder { @@ -14,7 +16,7 @@ namespace Microsoft.AspNetCore.Builder public static class ComponentEndpointRouteBuilderExtensions { /// - /// Maps the SignalR to the path . + /// Maps the Blazor to the default path. /// /// The . /// The . @@ -25,20 +27,41 @@ namespace Microsoft.AspNetCore.Builder throw new ArgumentNullException(nameof(endpoints)); } - return new ComponentEndpointConventionBuilder(endpoints.MapHub(ComponentHub.DefaultPath)); + return endpoints.MapBlazorHub(configureOptions: _ => { }); } /// - /// Maps the SignalR to the path and associates + /// Maps the Blazor to the default path. + /// + /// The . + /// A callback to configure dispatcher options. + /// The . + public static ComponentEndpointConventionBuilder MapBlazorHub(this IEndpointRouteBuilder endpoints, Action configureOptions) + { + if (endpoints == null) + { + throw new ArgumentNullException(nameof(endpoints)); + } + + if (configureOptions == null) + { + throw new ArgumentNullException(nameof(configureOptions)); + } + + return new ComponentEndpointConventionBuilder(endpoints.MapHub(ComponentHub.DefaultPath, configureOptions)); + } + + /// + ///Maps the Blazor to the default path and associates /// the component to this hub instance as the given DOM . /// - /// The first associated with this . + /// The first associated with this Blazor . /// The . /// The selector for the . /// The . public static ComponentEndpointConventionBuilder MapBlazorHub( this IEndpointRouteBuilder endpoints, - string selector) where TComponent: IComponent + string selector) where TComponent : IComponent { if (endpoints == null) { @@ -54,11 +77,43 @@ namespace Microsoft.AspNetCore.Builder } /// - /// Maps the SignalR to the path and associates + ///Maps the Blazor to the default path and associates + /// the component to this hub instance as the given DOM . + /// + /// The first associated with this Blazor . + /// The . + /// The selector for the . + /// A callback to configure dispatcher options. + /// The . + public static ComponentEndpointConventionBuilder MapBlazorHub( + this IEndpointRouteBuilder endpoints, + string selector, + Action configureOptions) where TComponent : IComponent + { + if (endpoints == null) + { + throw new ArgumentNullException(nameof(endpoints)); + } + + if (selector == null) + { + throw new ArgumentNullException(nameof(selector)); + } + + if (configureOptions == null) + { + throw new ArgumentNullException(nameof(configureOptions)); + } + + return endpoints.MapBlazorHub(typeof(TComponent), selector, ComponentHub.DefaultPath, configureOptions); + } + + /// + ///Maps the Blazor to the default path and associates /// the component to this hub instance as the given DOM . /// /// The . - /// The first associated with this . + /// The first associated with this Blazor . /// The selector for the component. /// The . public static ComponentEndpointConventionBuilder MapBlazorHub( @@ -85,13 +140,51 @@ namespace Microsoft.AspNetCore.Builder } /// - /// Maps the SignalR to the path and associates + ///Maps the Blazor to the default path and associates + /// the component to this hub instance as the given DOM . + /// + /// The . + /// The first associated with this Blazor . + /// The selector for the component. + /// A callback to configure dispatcher options. + /// The . + public static ComponentEndpointConventionBuilder MapBlazorHub( + this IEndpointRouteBuilder endpoints, + Type type, + string selector, + Action configureOptions) + { + if (endpoints == null) + { + throw new ArgumentNullException(nameof(endpoints)); + } + + if (type == null) + { + throw new ArgumentNullException(nameof(type)); + } + + if (selector == null) + { + throw new ArgumentNullException(nameof(selector)); + } + + if (configureOptions == null) + { + throw new ArgumentNullException(nameof(configureOptions)); + } + + return endpoints.MapBlazorHub(type, selector, ComponentHub.DefaultPath, configureOptions); + } + + /// + /// Maps the Blazor to the path and associates /// the component to this hub instance as the given DOM . /// - /// The first associated with this . + /// The first associated with this Blazor . /// The . /// The selector for the . - /// The path to map to which the will be mapped. + /// The path to map the Blazor . /// The . public static ComponentEndpointConventionBuilder MapBlazorHub( this IEndpointRouteBuilder endpoints, @@ -117,13 +210,52 @@ namespace Microsoft.AspNetCore.Builder } /// - /// Maps the SignalR to the path and associates + /// Maps the Blazor to the path and associates + /// the component to this hub instance as the given DOM . + /// + /// The first associated with this Blazor . + /// The . + /// The selector for the . + /// The path to map the Blazor . + /// A callback to configure dispatcher options. + /// The . + public static ComponentEndpointConventionBuilder MapBlazorHub( + this IEndpointRouteBuilder endpoints, + string selector, + string path, + Action configureOptions) where TComponent : IComponent + { + if (endpoints == null) + { + throw new ArgumentNullException(nameof(endpoints)); + } + + if (path == null) + { + throw new ArgumentNullException(nameof(path)); + } + + if (selector == null) + { + throw new ArgumentNullException(nameof(selector)); + } + + if (configureOptions == null) + { + throw new ArgumentNullException(nameof(configureOptions)); + } + + return endpoints.MapBlazorHub(typeof(TComponent), selector, path, configureOptions); + } + + /// + /// Maps the Blazor to the path and associates /// the component to this hub instance as the given DOM . /// /// The . - /// The first associated with this . + /// The first associated with this Blazor . /// The selector for the . - /// The path to map to which the will be mapped. + /// The path to map the Blazor . /// The . public static ComponentEndpointConventionBuilder MapBlazorHub( this IEndpointRouteBuilder endpoints, @@ -151,7 +283,52 @@ namespace Microsoft.AspNetCore.Builder throw new ArgumentNullException(nameof(selector)); } - return new ComponentEndpointConventionBuilder(endpoints.MapHub(path)).AddComponent(componentType, selector); + return endpoints.MapBlazorHub(componentType, selector, path, configureOptions: _ => { }); + } + + /// + /// Maps the Blazor to the path and associates + /// the component to this hub instance as the given DOM . + /// + /// The . + /// The first associated with this Blazor . + /// The selector for the . + /// A callback to configure dispatcher options. + /// The path to map the Blazor . + /// The . + public static ComponentEndpointConventionBuilder MapBlazorHub( + this IEndpointRouteBuilder endpoints, + Type componentType, + string selector, + string path, + Action configureOptions) + { + if (endpoints == null) + { + throw new ArgumentNullException(nameof(endpoints)); + } + + if (path == null) + { + throw new ArgumentNullException(nameof(path)); + } + + if (componentType == null) + { + throw new ArgumentNullException(nameof(componentType)); + } + + if (selector == null) + { + throw new ArgumentNullException(nameof(selector)); + } + + if (configureOptions == null) + { + throw new ArgumentNullException(nameof(configureOptions)); + } + + return new ComponentEndpointConventionBuilder(endpoints.MapHub(path, configureOptions)).AddComponent(componentType, selector); } } } diff --git a/src/Components/Server/test/ComponentEndpointRouteBuilderExtensionsTest.cs b/src/Components/Server/test/ComponentEndpointRouteBuilderExtensionsTest.cs new file mode 100644 index 0000000000..817ec5474d --- /dev/null +++ b/src/Components/Server/test/ComponentEndpointRouteBuilderExtensionsTest.cs @@ -0,0 +1,64 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Builder.Internal; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Moq; +using Xunit; + +namespace Microsoft.AspNetCore.Components.Server.Tests +{ + public class ComponentEndpointRouteBuilderExtensionsTest + { + [Fact] + public void MapBlazorHub_WiresUp_UnderlyingHub() + { + // Arrange + var applicationBuilder = new ApplicationBuilder( + new ServiceCollection() + .AddLogging() + .AddSingleton(Mock.Of()) + .AddSignalR().Services + .AddServerSideBlazor().Services.BuildServiceProvider()); + var called = false; + + // Act + var app = applicationBuilder + .UseRouting() + .UseEndpoints(endpoints => + { + endpoints.MapBlazorHub(dispatchOptions => called = true); + }).Build(); + + // Assert + Assert.True(called); + } + + [Fact] + public void MapBlazorHub_MostGeneralOverload_MapsUnderlyingHub() + { + // Arrange + var applicationBuilder = new ApplicationBuilder( + new ServiceCollection() + .AddLogging() + .AddSingleton(Mock.Of()) + .AddSignalR().Services + .AddServerSideBlazor().Services.BuildServiceProvider()); + var called = false; + + // Act + var app = applicationBuilder + .UseRouting() + .UseEndpoints(endpoints => + { + endpoints.MapBlazorHub(Mock.Of().GetType(),"app", "_blazor", dispatchOptions => called = true); + }).Build(); + + // Assert + Assert.True(called); + } + } +}