-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
AbpAspNetCoreSignalRModule.cs
152 lines (134 loc) · 4.72 KB
/
AbpAspNetCoreSignalRModule.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Volo.Abp.AspNetCore.Auditing;
using Volo.Abp.AspNetCore.SignalR.Auditing;
using Volo.Abp.AspNetCore.SignalR.Authentication;
using Volo.Abp.Auditing;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Modularity;
namespace Volo.Abp.AspNetCore.SignalR;
[DependsOn(
typeof(AbpAspNetCoreModule)
)]
public class AbpAspNetCoreSignalRModule : AbpModule
{
private static readonly MethodInfo MapHubGenericMethodInfo =
typeof(AbpAspNetCoreSignalRModule)
.GetMethod("MapHub", BindingFlags.Static | BindingFlags.NonPublic);
public override void PreConfigureServices(ServiceConfigurationContext context)
{
context.Services.AddConventionalRegistrar(new AbpSignalRConventionalRegistrar());
AutoAddHubTypes(context.Services);
}
public override void ConfigureServices(ServiceConfigurationContext context)
{
var routePatterns = new List<string> { "/signalr-hubs" };
var signalRServerBuilder = context.Services.AddSignalR(options =>
{
options.AddFilter<AbpHubContextAccessorHubFilter>();
options.AddFilter<AbpAuthenticationHubFilter>();
options.AddFilter<AbpAuditHubFilter>();
});
context.Services.ExecutePreConfiguredActions(signalRServerBuilder);
Configure<AbpEndpointRouterOptions>(options =>
{
options.EndpointConfigureActions.Add(endpointContext =>
{
var signalROptions = endpointContext
.ScopeServiceProvider
.GetRequiredService<IOptions<AbpSignalROptions>>()
.Value;
foreach (var hubConfig in signalROptions.Hubs)
{
routePatterns.AddIfNotContains(hubConfig.RoutePattern);
MapHubType(
hubConfig.HubType,
endpointContext.Endpoints,
hubConfig.RoutePattern,
opts =>
{
foreach (var configureAction in hubConfig.ConfigureActions)
{
configureAction(opts);
}
}
);
}
});
});
Configure<AbpAspNetCoreAuditingOptions>(options =>
{
foreach (var routePattern in routePatterns)
{
options.IgnoredUrls.AddIfNotContains(x => routePattern.StartsWith(x), () => routePattern);
}
});
Configure<AbpAuditingOptions>(options =>
{
options.Contributors.Add(new AspNetCoreSignalRAuditLogContributor());
});
}
private void AutoAddHubTypes(IServiceCollection services)
{
var hubTypes = new List<Type>();
services.OnRegistred(context =>
{
if (IsHubClass(context) && !IsDisabledForAutoMap(context))
{
hubTypes.Add(context.ImplementationType);
}
});
services.Configure<AbpSignalROptions>(options =>
{
foreach (var hubType in hubTypes)
{
options.Hubs.Add(HubConfig.Create(hubType));
}
});
}
private static bool IsHubClass(IOnServiceRegistredContext context)
{
return typeof(Hub).IsAssignableFrom(context.ImplementationType);
}
private static bool IsDisabledForAutoMap(IOnServiceRegistredContext context)
{
return context.ImplementationType.IsDefined(typeof(DisableAutoHubMapAttribute), true);
}
private void MapHubType(
Type hubType,
IEndpointRouteBuilder endpoints,
string pattern,
Action<HttpConnectionDispatcherOptions> configureOptions)
{
MapHubGenericMethodInfo
.MakeGenericMethod(hubType)
.Invoke(
this,
new object[]
{
endpoints,
pattern,
configureOptions
}
);
}
// ReSharper disable once UnusedMember.Local (used via reflection)
private static void MapHub<THub>(
IEndpointRouteBuilder endpoints,
string pattern,
Action<HttpConnectionDispatcherOptions> configureOptions)
where THub : Hub
{
endpoints.MapHub<THub>(
pattern,
configureOptions
);
}
}