/
OwinExtensions.cs
272 lines (237 loc) · 12.7 KB
/
OwinExtensions.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// 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.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Threading;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hosting;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Infrastructure;
using Microsoft.AspNet.SignalR.Owin.Middleware;
using Microsoft.AspNet.SignalR.Tracing;
using Microsoft.Owin.Infrastructure;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.Owin.Extensions;
using System.Globalization;
namespace Owin
{
[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Owin", Justification = "The owin namespace is for consistentcy.")]
public static class OwinExtensions
{
/// <summary>
/// Maps SignalR hubs to the app builder pipeline at "/signalr".
/// </summary>
/// <param name="builder">The app builder</param>
public static IAppBuilder MapSignalR(this IAppBuilder builder)
{
return builder.MapSignalR(new HubConfiguration());
}
/// <summary>
/// Maps SignalR hubs to the app builder pipeline at "/signalr".
/// </summary>
/// <param name="builder">The app builder</param>
/// <param name="configuration">The <see cref="HubConfiguration"/> to use</param>
public static IAppBuilder MapSignalR(this IAppBuilder builder, HubConfiguration configuration)
{
return builder.MapSignalR("/signalr", configuration);
}
/// <summary>
/// Maps SignalR hubs to the app builder pipeline at the specified path.
/// </summary>
/// <param name="builder">The app builder</param>
/// <param name="path">The path to map signalr hubs</param>
/// <param name="configuration">The <see cref="HubConfiguration"/> to use</param>
public static IAppBuilder MapSignalR(this IAppBuilder builder, string path, HubConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
return builder.Map(path, subApp => subApp.RunSignalR(configuration));
}
/// <summary>
/// Adds SignalR hubs to the app builder pipeline.
/// </summary>
/// <param name="builder">The app builder</param>
public static void RunSignalR(this IAppBuilder builder)
{
builder.RunSignalR(new HubConfiguration());
}
/// <summary>
/// Adds SignalR hubs to the app builder pipeline.
/// </summary>
/// <param name="builder">The app builder</param>
/// <param name="configuration">The <see cref="HubConfiguration"/> to use</param>
public static void RunSignalR(this IAppBuilder builder, HubConfiguration configuration)
{
builder.UseSignalRMiddleware<HubDispatcherMiddleware>(configuration);
}
/// <summary>
/// Maps the specified SignalR <see cref="PersistentConnection"/> to the app builder pipeline at
/// the specified path.
/// </summary>
/// <typeparam name="TConnection">The type of <see cref="PersistentConnection"/></typeparam>
/// <param name="builder">The app builder</param>
/// <param name="path">The path to map the <see cref="PersistentConnection"/></param>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is syntactic sugar")]
public static IAppBuilder MapSignalR<TConnection>(this IAppBuilder builder, string path) where TConnection : PersistentConnection
{
return builder.MapSignalR(path, typeof(TConnection), new ConnectionConfiguration());
}
/// <summary>
/// Maps the specified SignalR <see cref="PersistentConnection"/> to the app builder pipeline at
/// the specified path.
/// </summary>
/// <typeparam name="TConnection">The type of <see cref="PersistentConnection"/></typeparam>
/// <param name="builder">The app builder</param>
/// <param name="path">The path to map the persistent connection</param>
/// <param name="configuration">The <see cref="ConnectionConfiguration"/> to use</param>
/// <returns></returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is syntactic sugar")]
public static IAppBuilder MapSignalR<TConnection>(this IAppBuilder builder, string path, ConnectionConfiguration configuration) where TConnection : PersistentConnection
{
return builder.MapSignalR(path, typeof(TConnection), configuration);
}
/// <summary>
/// Maps the specified SignalR <see cref="PersistentConnection"/> to the app builder pipeline at
/// the specified path.
/// </summary>
/// <param name="builder">The app builder</param>
/// <param name="path">The path to map the persistent connection</param>
/// <param name="connectionType">The type of <see cref="PersistentConnection"/></param>
/// <param name="configuration">The <see cref="ConnectionConfiguration"/> to use</param>
public static IAppBuilder MapSignalR(this IAppBuilder builder, string path, Type connectionType, ConnectionConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}
return builder.Map(path, subApp => subApp.RunSignalR(connectionType, configuration));
}
/// <summary>
/// Adds the specified SignalR <see cref="PersistentConnection"/> to the app builder.
/// </summary>
/// <typeparam name="TConnection">The type of <see cref="PersistentConnection"/></typeparam>
/// <param name="builder">The app builder</param>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is syntactic sugar")]
public static void RunSignalR<TConnection>(this IAppBuilder builder) where TConnection : PersistentConnection
{
builder.RunSignalR<TConnection>(new ConnectionConfiguration());
}
/// <summary>
/// Adds the specified SignalR <see cref="PersistentConnection"/> to the app builder.
/// </summary>
/// <typeparam name="TConnection">The type of <see cref="PersistentConnection"/></typeparam>
/// <param name="builder">The app builder</param>
/// <param name="configuration">The <see cref="ConnectionConfiguration"/> to use</param>
/// <returns></returns>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is syntactic sugar")]
public static void RunSignalR<TConnection>(this IAppBuilder builder, ConnectionConfiguration configuration) where TConnection : PersistentConnection
{
builder.RunSignalR(typeof(TConnection), configuration);
}
/// <summary>
/// Adds the specified SignalR <see cref="PersistentConnection"/> to the app builder.
/// </summary>
/// <param name="builder">The app builder</param>
/// <param name="connectionType">The type of <see cref="PersistentConnection"/></param>
/// <param name="configuration">The <see cref="ConnectionConfiguration"/> to use</param>
/// <returns></returns>
public static void RunSignalR(this IAppBuilder builder, Type connectionType, ConnectionConfiguration configuration)
{
builder.UseSignalRMiddleware<PersistentConnectionMiddleware>(connectionType, configuration);
}
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "This class wires up new dependencies from the host")]
private static IAppBuilder UseSignalRMiddleware<T>(this IAppBuilder builder, params object[] args)
{
EnsureValidCulture();
ConnectionConfiguration configuration = null;
// Ensure we have the conversions for MS.Owin so that
// the app builder respects the OwinMiddleware base class
SignatureConversions.AddConversions(builder);
if (args.Length > 0)
{
configuration = args[args.Length - 1] as ConnectionConfiguration;
if (configuration == null)
{
throw new ArgumentException(Resources.Error_NoConfiguration);
}
var resolver = configuration.Resolver;
if (resolver == null)
{
throw new ArgumentException(Resources.Error_NoDependencyResolver);
}
var env = builder.Properties;
CancellationToken token = env.GetShutdownToken();
// If we don't get a valid instance name then generate a random one
string instanceName = env.GetAppInstanceName() ?? Guid.NewGuid().ToString();
// Use the data protection provider from app builder and fallback to the
// Dpapi provider
IDataProtectionProvider provider = builder.GetDataProtectionProvider();
IProtectedData protectedData;
// If we're using DPAPI then fallback to the default protected data if running
// on mono since it doesn't support any of this
if (provider == null && MonoUtility.IsRunningMono)
{
protectedData = new DefaultProtectedData();
}
else
{
if (provider == null)
{
provider = new DpapiDataProtectionProvider(instanceName);
}
protectedData = new DataProtectionProviderProtectedData(provider);
}
resolver.Register(typeof(IProtectedData), () => protectedData);
// If the host provides trace output then add a default trace listener
TextWriter traceOutput = env.GetTraceOutput();
if (traceOutput != null)
{
var hostTraceListener = new TextWriterTraceListener(traceOutput);
var traceManager = new TraceManager(hostTraceListener);
resolver.Register(typeof(ITraceManager), () => traceManager);
}
// Try to get the list of reference assemblies from the host
IEnumerable<Assembly> referenceAssemblies = env.GetReferenceAssemblies();
if (referenceAssemblies != null)
{
// Use this list as the assembly locator
var assemblyLocator = new EnumerableOfAssemblyLocator(referenceAssemblies);
resolver.Register(typeof(IAssemblyLocator), () => assemblyLocator);
}
resolver.InitializeHost(instanceName, token);
}
builder.Use(typeof(T), args);
// BUG 2306: We need to make that SignalR runs before any handlers are
// mapped in the IIS pipeline so that we avoid side effects like
// session being enabled. The session behavior can be
// manually overridden if user calls SetSessionStateBehavior but that shouldn't
// be a problem most of the time.
builder.UseStageMarker(PipelineStage.PostAuthorize);
return builder;
}
private static void EnsureValidCulture()
{
// The CultureInfo may leak across app domains which may cause hangs. The most prominent
// case in SignalR are MapSignalR hangs when creating Performance Counters (#3414).
// See https://github.com/SignalR/SignalR/issues/3414#issuecomment-152733194 for more details.
var culture = CultureInfo.CurrentCulture;
while (!culture.Equals(CultureInfo.InvariantCulture))
{
culture = culture.Parent;
}
if (ReferenceEquals(culture, CultureInfo.InvariantCulture))
{
return;
}
var thread = Thread.CurrentThread;
thread.CurrentCulture = CultureInfo.GetCultureInfo(thread.CurrentCulture.Name);
thread.CurrentUICulture = CultureInfo.GetCultureInfo(thread.CurrentUICulture.Name);
}
}
}