Skip to content

Commit 3c2e16b

Browse files
authored
Exposes the underlying HttpConnectionDispatcherOptions in MapBlazorHub (#10690)
* Expose new overloads to customize connection dispatch options
1 parent 3f041ce commit 3c2e16b

File tree

3 files changed

+260
-14
lines changed

3 files changed

+260
-14
lines changed

src/Components/Server/ref/Microsoft.AspNetCore.Components.Server.netcoreapp3.0.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ public static partial class ComponentEndpointConventionBuilderExtensions
1010
public static partial class ComponentEndpointRouteBuilderExtensions
1111
{
1212
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints) { throw null; }
13+
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Action<Microsoft.AspNetCore.Http.Connections.HttpConnectionDispatcherOptions> configureOptions) { throw null; }
1314
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Type type, string selector) { throw null; }
15+
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; }
1416
public static Microsoft.AspNetCore.Components.Server.ComponentEndpointConventionBuilder MapBlazorHub(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints, System.Type componentType, string selector, string path) { throw null; }
17+
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; }
1518
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; }
19+
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; }
1620
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; }
21+
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; }
1722
}
1823
}
1924
namespace Microsoft.AspNetCore.Components.Browser.Rendering

src/Components/Server/src/Builder/ComponentEndpointRouteBuilderExtensions.cs

Lines changed: 191 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System;
55
using Microsoft.AspNetCore.Components;
66
using Microsoft.AspNetCore.Components.Server;
7+
using Microsoft.AspNetCore.Http.Connections;
78
using Microsoft.AspNetCore.Routing;
9+
using Microsoft.AspNetCore.SignalR;
810

911
namespace Microsoft.AspNetCore.Builder
1012
{
@@ -14,7 +16,7 @@ namespace Microsoft.AspNetCore.Builder
1416
public static class ComponentEndpointRouteBuilderExtensions
1517
{
1618
/// <summary>
17-
/// Maps the SignalR <see cref="ComponentHub"/> to the path <see cref="ComponentHub.DefaultPath"/>.
19+
/// Maps the Blazor <see cref="Hub" /> to the default path.
1820
/// </summary>
1921
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
2022
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
@@ -25,20 +27,41 @@ public static ComponentEndpointConventionBuilder MapBlazorHub(this IEndpointRout
2527
throw new ArgumentNullException(nameof(endpoints));
2628
}
2729

28-
return new ComponentEndpointConventionBuilder(endpoints.MapHub<ComponentHub>(ComponentHub.DefaultPath));
30+
return endpoints.MapBlazorHub(configureOptions: _ => { });
2931
}
3032

3133
/// <summary>
32-
/// Maps the SignalR <see cref="ComponentHub"/> to the path <see cref="ComponentHub.DefaultPath"/> and associates
34+
/// Maps the Blazor <see cref="Hub" /> to the default path.
35+
/// </summary>
36+
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
37+
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
38+
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
39+
public static ComponentEndpointConventionBuilder MapBlazorHub(this IEndpointRouteBuilder endpoints, Action<HttpConnectionDispatcherOptions> configureOptions)
40+
{
41+
if (endpoints == null)
42+
{
43+
throw new ArgumentNullException(nameof(endpoints));
44+
}
45+
46+
if (configureOptions == null)
47+
{
48+
throw new ArgumentNullException(nameof(configureOptions));
49+
}
50+
51+
return new ComponentEndpointConventionBuilder(endpoints.MapHub<ComponentHub>(ComponentHub.DefaultPath, configureOptions));
52+
}
53+
54+
/// <summary>
55+
///Maps the Blazor <see cref="Hub" /> to the default path and associates
3356
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
3457
/// </summary>
35-
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</typeparam>
58+
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
3659
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
3760
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
3861
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
3962
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
4063
this IEndpointRouteBuilder endpoints,
41-
string selector) where TComponent: IComponent
64+
string selector) where TComponent : IComponent
4265
{
4366
if (endpoints == null)
4467
{
@@ -54,11 +77,43 @@ public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
5477
}
5578

5679
/// <summary>
57-
/// Maps the SignalR <see cref="ComponentHub"/> to the path <see cref="ComponentHub.DefaultPath"/> and associates
80+
///Maps the Blazor <see cref="Hub" /> to the default path and associates
81+
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
82+
/// </summary>
83+
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
84+
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
85+
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
86+
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
87+
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
88+
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
89+
this IEndpointRouteBuilder endpoints,
90+
string selector,
91+
Action<HttpConnectionDispatcherOptions> configureOptions) where TComponent : IComponent
92+
{
93+
if (endpoints == null)
94+
{
95+
throw new ArgumentNullException(nameof(endpoints));
96+
}
97+
98+
if (selector == null)
99+
{
100+
throw new ArgumentNullException(nameof(selector));
101+
}
102+
103+
if (configureOptions == null)
104+
{
105+
throw new ArgumentNullException(nameof(configureOptions));
106+
}
107+
108+
return endpoints.MapBlazorHub(typeof(TComponent), selector, ComponentHub.DefaultPath, configureOptions);
109+
}
110+
111+
/// <summary>
112+
///Maps the Blazor <see cref="Hub" /> to the default path and associates
58113
/// the component <paramref name="type"/> to this hub instance as the given DOM <paramref name="selector"/>.
59114
/// </summary>
60115
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
61-
/// <param name="type">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</param>
116+
/// <param name="type">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
62117
/// <param name="selector">The selector for the component.</param>
63118
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
64119
public static ComponentEndpointConventionBuilder MapBlazorHub(
@@ -85,13 +140,51 @@ public static ComponentEndpointConventionBuilder MapBlazorHub(
85140
}
86141

87142
/// <summary>
88-
/// Maps the SignalR <see cref="ComponentHub"/> to the path <paramref name="path"/> and associates
143+
///Maps the Blazor <see cref="Hub" /> to the default path and associates
144+
/// the component <paramref name="type"/> to this hub instance as the given DOM <paramref name="selector"/>.
145+
/// </summary>
146+
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
147+
/// <param name="type">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
148+
/// <param name="selector">The selector for the component.</param>
149+
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
150+
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
151+
public static ComponentEndpointConventionBuilder MapBlazorHub(
152+
this IEndpointRouteBuilder endpoints,
153+
Type type,
154+
string selector,
155+
Action<HttpConnectionDispatcherOptions> configureOptions)
156+
{
157+
if (endpoints == null)
158+
{
159+
throw new ArgumentNullException(nameof(endpoints));
160+
}
161+
162+
if (type == null)
163+
{
164+
throw new ArgumentNullException(nameof(type));
165+
}
166+
167+
if (selector == null)
168+
{
169+
throw new ArgumentNullException(nameof(selector));
170+
}
171+
172+
if (configureOptions == null)
173+
{
174+
throw new ArgumentNullException(nameof(configureOptions));
175+
}
176+
177+
return endpoints.MapBlazorHub(type, selector, ComponentHub.DefaultPath, configureOptions);
178+
}
179+
180+
/// <summary>
181+
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
89182
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
90183
/// </summary>
91-
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</typeparam>
184+
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
92185
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
93186
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
94-
/// <param name="path">The path to map to which the <see cref="ComponentHub"/> will be mapped.</param>
187+
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
95188
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
96189
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
97190
this IEndpointRouteBuilder endpoints,
@@ -117,13 +210,52 @@ public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
117210
}
118211

119212
/// <summary>
120-
/// Maps the SignalR <see cref="ComponentHub"/> to the path <paramref name="path"/> and associates
213+
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
214+
/// the component <typeparamref name="TComponent"/> to this hub instance as the given DOM <paramref name="selector"/>.
215+
/// </summary>
216+
/// <typeparam name="TComponent">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</typeparam>
217+
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
218+
/// <param name="selector">The selector for the <typeparamref name="TComponent"/>.</param>
219+
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
220+
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
221+
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
222+
public static ComponentEndpointConventionBuilder MapBlazorHub<TComponent>(
223+
this IEndpointRouteBuilder endpoints,
224+
string selector,
225+
string path,
226+
Action<HttpConnectionDispatcherOptions> configureOptions) where TComponent : IComponent
227+
{
228+
if (endpoints == null)
229+
{
230+
throw new ArgumentNullException(nameof(endpoints));
231+
}
232+
233+
if (path == null)
234+
{
235+
throw new ArgumentNullException(nameof(path));
236+
}
237+
238+
if (selector == null)
239+
{
240+
throw new ArgumentNullException(nameof(selector));
241+
}
242+
243+
if (configureOptions == null)
244+
{
245+
throw new ArgumentNullException(nameof(configureOptions));
246+
}
247+
248+
return endpoints.MapBlazorHub(typeof(TComponent), selector, path, configureOptions);
249+
}
250+
251+
/// <summary>
252+
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
121253
/// the component <paramref name="componentType"/> to this hub instance as the given DOM <paramref name="selector"/>.
122254
/// </summary>
123255
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
124-
/// <param name="componentType">The first <see cref="IComponent"/> associated with this <see cref="ComponentHub"/>.</param>
256+
/// <param name="componentType">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
125257
/// <param name="selector">The selector for the <paramref name="componentType"/>.</param>
126-
/// <param name="path">The path to map to which the <see cref="ComponentHub"/> will be mapped.</param>
258+
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
127259
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
128260
public static ComponentEndpointConventionBuilder MapBlazorHub(
129261
this IEndpointRouteBuilder endpoints,
@@ -151,7 +283,52 @@ public static ComponentEndpointConventionBuilder MapBlazorHub(
151283
throw new ArgumentNullException(nameof(selector));
152284
}
153285

154-
return new ComponentEndpointConventionBuilder(endpoints.MapHub<ComponentHub>(path)).AddComponent(componentType, selector);
286+
return endpoints.MapBlazorHub(componentType, selector, path, configureOptions: _ => { });
287+
}
288+
289+
/// <summary>
290+
/// Maps the Blazor <see cref="Hub" /> to the path <paramref name="path"/> and associates
291+
/// the component <paramref name="componentType"/> to this hub instance as the given DOM <paramref name="selector"/>.
292+
/// </summary>
293+
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/>.</param>
294+
/// <param name="componentType">The first <see cref="IComponent"/> associated with this Blazor <see cref="Hub" />.</param>
295+
/// <param name="selector">The selector for the <paramref name="componentType"/>.</param>
296+
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
297+
/// <param name="path">The path to map the Blazor <see cref="Hub" />.</param>
298+
/// <returns>The <see cref="ComponentEndpointConventionBuilder"/>.</returns>
299+
public static ComponentEndpointConventionBuilder MapBlazorHub(
300+
this IEndpointRouteBuilder endpoints,
301+
Type componentType,
302+
string selector,
303+
string path,
304+
Action<HttpConnectionDispatcherOptions> configureOptions)
305+
{
306+
if (endpoints == null)
307+
{
308+
throw new ArgumentNullException(nameof(endpoints));
309+
}
310+
311+
if (path == null)
312+
{
313+
throw new ArgumentNullException(nameof(path));
314+
}
315+
316+
if (componentType == null)
317+
{
318+
throw new ArgumentNullException(nameof(componentType));
319+
}
320+
321+
if (selector == null)
322+
{
323+
throw new ArgumentNullException(nameof(selector));
324+
}
325+
326+
if (configureOptions == null)
327+
{
328+
throw new ArgumentNullException(nameof(configureOptions));
329+
}
330+
331+
return new ComponentEndpointConventionBuilder(endpoints.MapHub<ComponentHub>(path, configureOptions)).AddComponent(componentType, selector);
155332
}
156333
}
157334
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.AspNetCore.Builder.Internal;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Hosting;
9+
using Moq;
10+
using Xunit;
11+
12+
namespace Microsoft.AspNetCore.Components.Server.Tests
13+
{
14+
public class ComponentEndpointRouteBuilderExtensionsTest
15+
{
16+
[Fact]
17+
public void MapBlazorHub_WiresUp_UnderlyingHub()
18+
{
19+
// Arrange
20+
var applicationBuilder = new ApplicationBuilder(
21+
new ServiceCollection()
22+
.AddLogging()
23+
.AddSingleton(Mock.Of<IHostApplicationLifetime>())
24+
.AddSignalR().Services
25+
.AddServerSideBlazor().Services.BuildServiceProvider());
26+
var called = false;
27+
28+
// Act
29+
var app = applicationBuilder
30+
.UseRouting()
31+
.UseEndpoints(endpoints =>
32+
{
33+
endpoints.MapBlazorHub(dispatchOptions => called = true);
34+
}).Build();
35+
36+
// Assert
37+
Assert.True(called);
38+
}
39+
40+
[Fact]
41+
public void MapBlazorHub_MostGeneralOverload_MapsUnderlyingHub()
42+
{
43+
// Arrange
44+
var applicationBuilder = new ApplicationBuilder(
45+
new ServiceCollection()
46+
.AddLogging()
47+
.AddSingleton(Mock.Of<IHostApplicationLifetime>())
48+
.AddSignalR().Services
49+
.AddServerSideBlazor().Services.BuildServiceProvider());
50+
var called = false;
51+
52+
// Act
53+
var app = applicationBuilder
54+
.UseRouting()
55+
.UseEndpoints(endpoints =>
56+
{
57+
endpoints.MapBlazorHub(Mock.Of<IComponent>().GetType(),"app", "_blazor", dispatchOptions => called = true);
58+
}).Build();
59+
60+
// Assert
61+
Assert.True(called);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)