Exposes the underlying HttpConnectionDispatcherOptions in MapBlazorHub (#10690)

* Expose new overloads to customize connection dispatch options
This commit is contained in:
Javier Calvarro Nelson 2019-05-31 22:25:08 +02:00 committed by GitHub
parent 3f041cee44
commit 3c2e16bf92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 260 additions and 14 deletions

View File

@ -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<Microsoft.AspNetCore.Http.Connections.HttpConnectionDispatcherOptions> 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<Microsoft.AspNetCore.Http.Connections.HttpConnectionDispatcherOptions> 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<Microsoft.AspNetCore.Http.Connections.HttpConnectionDispatcherOptions> configureOptions) { throw null; }
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; }
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector, System.Action<Microsoft.AspNetCore.Http.Connections.HttpConnectionDispatcherOptions> configureOptions) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; }
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(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<TComponent>(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, string selector, string path, System.Action<Microsoft.AspNetCore.Http.Connections.HttpConnectionDispatcherOptions> configureOptions) where TComponent : Microsoft.AspNetCore.Components.IComponent { throw null; }
}
}
namespace Microsoft.AspNetCore.Components.Browser.Rendering

View File

@ -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
{
/// <summary>
/// Maps the SignalR <see cref="ComponentHub"/> to the path <see cref="ComponentHub.DefaultPath"/>.
/// Maps the Blazor <see cref="Hub" /> to the default path.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
@ -25,20 +27,41 @@ namespace Microsoft.AspNetCore.Builder
throw new ArgumentNullException(nameof(endpoints));
}
return new ComponentEndpointConventionBuilder(endpoints.MapHub<ComponentHub>(ComponentHub.DefaultPath));
return endpoints.MapBlazorHub(configureOptions: _ => { });
}
/// <summary>
/// Maps the SignalR <see cref="ComponentHub"/> to the path <see cref="ComponentHub.DefaultPath"/> and associates
/// Maps the Blazor <see cref="Hub" /> to the default path.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub(this IEndpointRouteBuilder endpoints, Action<HttpConnectionDispatcherOptions> configureOptions)
{
if (endpoints == null)
{
throw new ArgumentNullException(nameof(endpoints));
}
if (configureOptions == null)
{
throw new ArgumentNullException(nameof(configureOptions));
}
return new ComponentEndpointConventionBuilder(endpoints.MapHub<ComponentHub>(ComponentHub.DefaultPath, configureOptions));
}
/// <summary>
///Maps the Blazor <see cref="Hub" /> to the default path and associates
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</typeparam>
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
this IEndpointRouteBuilder endpoints,
string selector) where TComponent: IComponent
string selector) where TComponent : IComponent
{
if (endpoints == null)
{
@ -54,11 +77,43 @@ namespace Microsoft.AspNetCore.Builder
}
/// <summary>
/// Maps the SignalR <see cref="ComponentHub"/> to the path <see cref="ComponentHub.DefaultPath"/> and associates
///Maps the Blazor <see cref="Hub" /> to the default path and associates
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
this IEndpointRouteBuilder endpoints,
string selector,
Action<HttpConnectionDispatcherOptions> 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);
}
/// <summary>
///Maps the Blazor <see cref="Hub" /> to the default path and associates
/// the component <paramref name="type"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="type">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</param>
/// <param name="type">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
/// <param name="selector">The selector for the component.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub(
@ -85,13 +140,51 @@ namespace Microsoft.AspNetCore.Builder
}
/// <summary>
/// Maps the SignalR <see cref="ComponentHub"/> to the path <paramref name="path"/> and associates
///Maps the Blazor <see cref="Hub" /> to the default path and associates
/// the component <paramref name="type"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="type">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
/// <param name="selector">The selector for the component.</param>
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub(
this IEndpointRouteBuilder endpoints,
Type type,
string selector,
Action<HttpConnectionDispatcherOptions> 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);
}
/// <summary>
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</typeparam>
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
/// <param name="path">The path to map to which the <see cref="ComponentHub"/> will be mapped.</param>
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
this IEndpointRouteBuilder endpoints,
@ -117,13 +210,52 @@ namespace Microsoft.AspNetCore.Builder
}
/// <summary>
/// Maps the SignalR <see cref="ComponentHub"/> to the path <paramref name="path"/> and associates
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
this IEndpointRouteBuilder endpoints,
string selector,
string path,
Action<HttpConnectionDispatcherOptions> 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);
}
/// <summary>
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
/// the component <paramref name="componentType"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="componentType">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</param>
/// <param name="componentType">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
/// <param name="selector">The selector for the <paramref name="componentType"/>.</param>
/// <param name="path">The path to map to which the <see cref="ComponentHub"/> will be mapped.</param>
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
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<ComponentHub>(path)).AddComponent(componentType, selector);
return endpoints.MapBlazorHub(componentType, selector, path, configureOptions: _ => { });
}
/// <summary>
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
/// the component <paramref name="componentType"/> to this hub instance as the given DOM <paramref name="selector"/>.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
/// <param name="componentType">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
/// <param name="selector">The selector for the <paramref name="componentType"/>.</param>
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
public static ComponentEndpointConventionBuilder MapBlazorHub(
this IEndpointRouteBuilder endpoints,
Type componentType,
string selector,
string path,
Action<HttpConnectionDispatcherOptions> 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<ComponentHub>(path, configureOptions)).AddComponent(componentType, selector);
}
}
}

View File

@ -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<IHostApplicationLifetime>())
.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<IHostApplicationLifetime>())
.AddSignalR().Services
.AddServerSideBlazor().Services.BuildServiceProvider());
var called = false;
// Act
var app = applicationBuilder
.UseRouting()
.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub(Mock.Of<IComponent>().GetType(),"app", "_blazor", dispatchOptions => called = true);
}).Build();
// Assert
Assert.True(called);
}
}
}