This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 446
/
HubRouteBuilder.cs
61 lines (55 loc) · 2.33 KB
/
HubRouteBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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 System;
using System.Reflection;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Connections;
namespace Microsoft.AspNetCore.SignalR
{
/// <summary>
/// Maps incoming requests to <see cref="Hub"/> types.
/// </summary>
public class HubRouteBuilder
{
private readonly ConnectionsRouteBuilder _routes;
/// <summary>
/// Initializes a new instance of the <see cref="HubRouteBuilder"/> class.
/// </summary>
/// <param name="routes">The routes builder.</param>
public HubRouteBuilder(ConnectionsRouteBuilder routes)
{
_routes = routes;
}
/// <summary>
/// Maps incoming requests with the specified path to the specified <see cref="Hub"/> type.
/// </summary>
/// <typeparam name="THub">The <see cref="Hub"/> type to map requests to.</typeparam>
/// <param name="path">The request path.</param>
public void MapHub<THub>(PathString path) where THub : Hub
{
MapHub<THub>(path, configureOptions: null);
}
/// <summary>
/// Maps incoming requests with the specified path to the specified <see cref="Hub"/> type.
/// </summary>
/// <typeparam name="THub">The <see cref="Hub"/> type to map requests to.</typeparam>
/// <param name="path">The request path.</param>
/// <param name="configureOptions">A callback to configure dispatcher options.</param>
public void MapHub<THub>(PathString path, Action<HttpConnectionDispatcherOptions> configureOptions) where THub : Hub
{
// find auth attributes
var authorizeAttributes = typeof(THub).GetCustomAttributes<AuthorizeAttribute>(inherit: true);
var options = new HttpConnectionDispatcherOptions();
foreach (var attribute in authorizeAttributes)
{
options.AuthorizationData.Add(attribute);
}
configureOptions?.Invoke(options);
_routes.MapConnections(path, options, builder =>
{
builder.UseHub<THub>();
});
}
}
}