Skip to content

Commit c85d231

Browse files
committed
hubmetadata
1 parent e42a014 commit c85d231

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

src/SignalR/server/SignalR/src/HubRouteBuilder.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using System;
55
using System.Reflection;
66
using Microsoft.AspNetCore.Authorization;
7+
using Microsoft.AspNetCore.Builder;
78
using Microsoft.AspNetCore.Http;
89
using Microsoft.AspNetCore.Http.Connections;
10+
using Microsoft.AspNetCore.Routing;
911

1012
namespace Microsoft.AspNetCore.SignalR
1113
{
@@ -15,6 +17,7 @@ namespace Microsoft.AspNetCore.SignalR
1517
public class HubRouteBuilder
1618
{
1719
private readonly ConnectionsRouteBuilder _routes;
20+
private readonly IEndpointRouteBuilder _endpoints;
1821

1922
/// <summary>
2023
/// Initializes a new instance of the <see cref="HubRouteBuilder"/> class.
@@ -25,6 +28,11 @@ public HubRouteBuilder(ConnectionsRouteBuilder routes)
2528
_routes = routes;
2629
}
2730

31+
internal HubRouteBuilder(IEndpointRouteBuilder endpoints)
32+
{
33+
_endpoints = endpoints;
34+
}
35+
2836
/// <summary>
2937
/// Maps incoming requests with the specified path to the specified <see cref="Hub"/> type.
3038
/// </summary>
@@ -43,6 +51,12 @@ public void MapHub<THub>(PathString path) where THub : Hub
4351
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
4452
public void MapHub<THub>(PathString path, Action<HttpConnectionDispatcherOptions> configureOptions) where THub : Hub
4553
{
54+
if (_endpoints != null)
55+
{
56+
_endpoints.MapHub<THub>(path, configureOptions);
57+
return;
58+
}
59+
4660
// find auth attributes
4761
var authorizeAttributes = typeof(THub).GetCustomAttributes<AuthorizeAttribute>(inherit: true);
4862
var options = new HttpConnectionDispatcherOptions();
@@ -52,8 +66,6 @@ public void MapHub<THub>(PathString path, Action<HttpConnectionDispatcherOptions
5266
}
5367
configureOptions?.Invoke(options);
5468

55-
// TODO: Figure out how to add the HubMetadata to endpoints
56-
5769
_routes.MapConnections(path, options, builder =>
5870
{
5971
builder.UseHub<THub>();

src/SignalR/server/SignalR/src/SignalRAppBuilderExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ public static IApplicationBuilder UseSignalR(this IApplicationBuilder app, Actio
2727
"'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code.");
2828
}
2929

30-
app.UseConnections(routes =>
30+
app.UseWebSockets();
31+
app.UseRouting();
32+
app.UseAuthorization();
33+
app.UseEndpoints(endpoints =>
3134
{
32-
configure(new HubRouteBuilder(routes));
35+
configure(new HubRouteBuilder(endpoints));
3336
});
3437

3538
return app;

src/SignalR/server/SignalR/test/MapSignalRTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public void MapHubFindsAuthAttributeOnHub()
114114
Assert.Equal(1, dataSource.Endpoints[1].Metadata.GetOrderedMetadata<IAuthorizeData>().Count);
115115
}
116116

117-
Assert.Equal(1, authCount);
117+
Assert.Equal(0, authCount);
118118
}
119119

120120
[Fact]
@@ -135,7 +135,7 @@ public void MapHubFindsAuthAttributeOnInheritedHub()
135135
Assert.Equal(1, dataSource.Endpoints[1].Metadata.GetOrderedMetadata<IAuthorizeData>().Count);
136136
}
137137

138-
Assert.Equal(1, authCount);
138+
Assert.Equal(0, authCount);
139139
}
140140

141141
[Fact]
@@ -156,7 +156,7 @@ public void MapHubFindsMultipleAuthAttributesOnDoubleAuthHub()
156156
Assert.Equal(2, dataSource.Endpoints[1].Metadata.GetOrderedMetadata<IAuthorizeData>().Count);
157157
}
158158

159-
Assert.Equal(2, authCount);
159+
Assert.Equal(0, authCount);
160160
}
161161

162162
[Fact]
@@ -266,9 +266,8 @@ void ConfigureRoutes(HubRouteBuilder routes)
266266
// We register 2 endpoints (/negotiate and /)
267267
Assert.Equal(2, dataSource.Endpoints.Count);
268268

269-
// TODO
270-
//Assert.Equal(typeof(AuthHub), dataSource.Endpoints[0].Metadata.GetMetadata<HubMetadata>()?.HubType);
271-
//Assert.Equal(typeof(AuthHub), dataSource.Endpoints[1].Metadata.GetMetadata<HubMetadata>()?.HubType);
269+
Assert.Equal(typeof(AuthHub), dataSource.Endpoints[0].Metadata.GetMetadata<HubMetadata>()?.HubType);
270+
Assert.Equal(typeof(AuthHub), dataSource.Endpoints[1].Metadata.GetMetadata<HubMetadata>()?.HubType);
272271
Assert.NotNull(dataSource.Endpoints[0].Metadata.GetMetadata<NegotiateMetadata>());
273272
Assert.Null(dataSource.Endpoints[1].Metadata.GetMetadata<NegotiateMetadata>());
274273
}

0 commit comments

Comments
 (0)