Skip to content

Commit a1c226e

Browse files
[SignalR] Move to generic host (#22602)
1 parent 6a4d79c commit a1c226e

File tree

17 files changed

+305
-254
lines changed

17 files changed

+305
-254
lines changed

src/Hosting/Hosting/ref/Microsoft.AspNetCore.Hosting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<Reference Include="Microsoft.Extensions.DependencyInjection" />
1717
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
1818
<Reference Include="Microsoft.Extensions.FileProviders.Composite" />
19-
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
19+
<Reference Include="Microsoft.Extensions.Hosting" />
2020
<Reference Include="Microsoft.Extensions.Logging" />
2121
<Reference Include="Microsoft.Extensions.Options" />
2222
</ItemGroup>

src/Hosting/Hosting/src/Microsoft.AspNetCore.Hosting.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core hosting infrastructure and startup logic for web applications.</Description>
@@ -26,7 +26,7 @@
2626
<Reference Include="Microsoft.Extensions.DependencyInjection" />
2727
<Reference Include="Microsoft.Extensions.FileProviders.Physical" />
2828
<Reference Include="Microsoft.Extensions.FileProviders.Composite" />
29-
<Reference Include="Microsoft.Extensions.Hosting.Abstractions" />
29+
<Reference Include="Microsoft.Extensions.Hosting" />
3030
<Reference Include="Microsoft.Extensions.Logging" />
3131
<Reference Include="Microsoft.Extensions.Options" />
3232

src/SignalR/clients/csharp/Client/test/FunctionalTests/HubConnectionTests.cs

Lines changed: 54 additions & 54 deletions
Large diffs are not rendered by default.

src/SignalR/clients/csharp/Client/test/FunctionalTests/HubProtocolVersionTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class HubProtocolVersionTests : FunctionalTestBase
3535
[MemberData(nameof(TransportTypes))]
3636
public async Task ClientUsingOldCallWithOriginalProtocol(HttpTransportType transportType)
3737
{
38-
using (var server = await StartServer<VersionStartup>())
38+
await using (var server = await StartServer<VersionStartup>())
3939
{
4040
var connectionBuilder = new HubConnectionBuilder()
4141
.WithLoggerFactory(LoggerFactory)
@@ -67,7 +67,7 @@ public async Task ClientUsingOldCallWithOriginalProtocol(HttpTransportType trans
6767
[MemberData(nameof(TransportTypes))]
6868
public async Task ClientUsingOldCallWithNewProtocol(HttpTransportType transportType)
6969
{
70-
using (var server = await StartServer<VersionStartup>())
70+
await using (var server = await StartServer<VersionStartup>())
7171
{
7272
var connectionBuilder = new HubConnectionBuilder()
7373
.WithLoggerFactory(LoggerFactory)
@@ -100,7 +100,7 @@ public async Task ClientUsingOldCallWithNewProtocol(HttpTransportType transportT
100100
[MemberData(nameof(TransportTypes))]
101101
public async Task ClientUsingNewCallWithNewProtocol(HttpTransportType transportType)
102102
{
103-
using (var server = await StartServer<VersionStartup>())
103+
await using (var server = await StartServer<VersionStartup>())
104104
{
105105
var httpConnectionFactory = new HttpConnectionFactory(
106106
Options.Create(new HttpConnectionOptions
@@ -166,7 +166,7 @@ bool ExpectedErrors(WriteContext writeContext)
166166
return writeContext.LoggerName == typeof(HubConnection).FullName;
167167
}
168168

169-
using (var server = await StartServer<VersionStartup>(ExpectedErrors))
169+
await using (var server = await StartServer<VersionStartup>(ExpectedErrors))
170170
{
171171
var connectionBuilder = new HubConnectionBuilder()
172172
.WithLoggerFactory(LoggerFactory)

src/SignalR/clients/ts/FunctionalTests/Program.cs

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
using System.IO;
66
using System.Runtime.InteropServices;
77
using System.Security.Cryptography.X509Certificates;
8+
using System.Threading.Tasks;
89
using Microsoft.AspNetCore.Hosting;
10+
using Microsoft.Extensions.Hosting;
911
using Microsoft.Extensions.Logging;
1012
using Microsoft.Win32;
1113

1214
namespace FunctionalTests
1315
{
1416
public class Program
1517
{
16-
public static void Main(string[] args)
18+
public static Task Main(string[] args)
1719
{
1820
string url = null;
1921
for (var i = 0; i < args.Length; i++)
@@ -27,65 +29,69 @@ public static void Main(string[] args)
2729
}
2830
}
2931

30-
var hostBuilder = new WebHostBuilder()
31-
.ConfigureLogging(factory =>
32+
var hostBuilder = new HostBuilder()
33+
.ConfigureWebHost(webHostBuilder =>
3234
{
33-
factory.AddConsole(options =>
35+
webHostBuilder
36+
.ConfigureLogging(factory =>
3437
{
35-
options.IncludeScopes = true;
36-
options.TimestampFormat = "[HH:mm:ss] ";
37-
options.UseUtcTimestamp = true;
38-
});
39-
factory.AddDebug();
40-
factory.SetMinimumLevel(LogLevel.Debug);
41-
})
42-
.UseKestrel((builderContext, options) =>
43-
{
44-
options.ConfigureHttpsDefaults(httpsOptions =>
38+
factory.AddConsole(options =>
39+
{
40+
options.IncludeScopes = true;
41+
options.TimestampFormat = "[HH:mm:ss] ";
42+
options.UseUtcTimestamp = true;
43+
});
44+
factory.AddDebug();
45+
factory.SetMinimumLevel(LogLevel.Debug);
46+
})
47+
.UseKestrel((builderContext, options) =>
4548
{
46-
bool useRSA = false;
47-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
49+
options.ConfigureHttpsDefaults(httpsOptions =>
4850
{
49-
// Detect Win10+
50-
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
51-
var major = key.GetValue("CurrentMajorVersionNumber") as int?;
52-
var minor = key.GetValue("CurrentMinorVersionNumber") as int?;
51+
bool useRSA = false;
52+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
53+
{
54+
// Detect Win10+
55+
var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
56+
var major = key.GetValue("CurrentMajorVersionNumber") as int?;
57+
var minor = key.GetValue("CurrentMinorVersionNumber") as int?;
5358

54-
if (major.HasValue && minor.HasValue)
59+
if (major.HasValue && minor.HasValue)
60+
{
61+
useRSA = true;
62+
}
63+
}
64+
else
5565
{
5666
useRSA = true;
5767
}
58-
}
59-
else
60-
{
61-
useRSA = true;
62-
}
6368

64-
if (useRSA)
65-
{
66-
// RSA cert, won't work on Windows 8.1 & Windows 2012 R2 using HTTP2, and ECC won't work in some Node environments
67-
var certPath = Path.Combine(Directory.GetCurrentDirectory(), "testCert.pfx");
68-
httpsOptions.ServerCertificate = new X509Certificate2(certPath, "testPassword");
69-
}
70-
else
71-
{
72-
// ECC cert, works on Windows 8.1 & Windows 2012 R2 using HTTP2
73-
var certPath = Path.Combine(Directory.GetCurrentDirectory(), "testCertECC.pfx");
74-
httpsOptions.ServerCertificate = new X509Certificate2(certPath, "testPassword");
75-
}
76-
});
77-
})
78-
.UseContentRoot(Directory.GetCurrentDirectory())
79-
.UseIISIntegration()
80-
.UseStartup<Startup>();
69+
if (useRSA)
70+
{
71+
// RSA cert, won't work on Windows 8.1 & Windows 2012 R2 using HTTP2, and ECC won't work in some Node environments
72+
var certPath = Path.Combine(Directory.GetCurrentDirectory(), "testCert.pfx");
73+
httpsOptions.ServerCertificate = new X509Certificate2(certPath, "testPassword");
74+
}
75+
else
76+
{
77+
// ECC cert, works on Windows 8.1 & Windows 2012 R2 using HTTP2
78+
var certPath = Path.Combine(Directory.GetCurrentDirectory(), "testCertECC.pfx");
79+
httpsOptions.ServerCertificate = new X509Certificate2(certPath, "testPassword");
80+
}
81+
});
82+
})
83+
.UseContentRoot(Directory.GetCurrentDirectory())
84+
.UseIISIntegration()
85+
.UseStartup<Startup>();
8186

82-
if (!string.IsNullOrEmpty(url))
83-
{
84-
Console.WriteLine($"Forcing URL to: {url}");
85-
hostBuilder.UseUrls(url);
86-
}
87+
if (!string.IsNullOrEmpty(url))
88+
{
89+
Console.WriteLine($"Forcing URL to: {url}");
90+
webHostBuilder.UseUrls(url);
91+
}
92+
});
8793

88-
hostBuilder.Build().Run();
94+
return hostBuilder.Build().RunAsync();
8995
}
9096
}
9197
}

src/SignalR/common/Http.Connections/test/MapConnectionHandlerTests.cs

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
using Microsoft.AspNetCore.Cors;
1414
using Microsoft.AspNetCore.Cors.Infrastructure;
1515
using Microsoft.AspNetCore.Hosting;
16+
using Microsoft.AspNetCore.Hosting.Server;
1617
using Microsoft.AspNetCore.Hosting.Server.Features;
1718
using Microsoft.AspNetCore.Routing;
1819
using Microsoft.AspNetCore.SignalR.Tests;
1920
using Microsoft.AspNetCore.Testing;
2021
using Microsoft.Extensions.DependencyInjection;
22+
using Microsoft.Extensions.Hosting;
2123
using Microsoft.Extensions.Logging;
2224
using Xunit;
2325
using Xunit.Abstractions;
@@ -305,7 +307,7 @@ void ConfigureRoutes(IEndpointRouteBuilder endpoints)
305307
[WebSocketsSupportedCondition]
306308
public async Task MapConnectionHandlerWithWebSocketSubProtocolSetsProtocol()
307309
{
308-
var host = BuildWebHost<MyConnectionHandler>("/socket",
310+
using var host = BuildWebHost<MyConnectionHandler>("/socket",
309311
options => options.WebSockets.SubProtocolSelector = subprotocols =>
310312
{
311313
Assert.Equal(new[] { "protocol1", "protocol2" }, subprotocols.ToArray());
@@ -314,7 +316,7 @@ public async Task MapConnectionHandlerWithWebSocketSubProtocolSetsProtocol()
314316

315317
await host.StartAsync();
316318

317-
var feature = host.ServerFeatures.Get<IServerAddressesFeature>();
319+
var feature = host.Services.GetService<IServer>().Features.Get<IServerAddressesFeature>();
318320
var address = feature.Addresses.First().Replace("http", "ws") + "/socket";
319321

320322
var client = new ClientWebSocket();
@@ -377,44 +379,52 @@ public override Task OnConnectedAsync(ConnectionContext connection)
377379
}
378380
}
379381

380-
private IWebHost BuildWebHost(Action<IEndpointRouteBuilder> configure)
382+
private IHost BuildWebHost(Action<IEndpointRouteBuilder> configure)
381383
{
382-
return new WebHostBuilder()
383-
.UseKestrel()
384-
.ConfigureServices(services =>
384+
return new HostBuilder()
385+
.ConfigureWebHost(webHostBuilder =>
385386
{
386-
services.AddConnections();
387-
})
388-
.Configure(app =>
389-
{
390-
app.UseRouting();
391-
app.UseEndpoints(endpoints => configure(endpoints));
387+
webHostBuilder
388+
.UseKestrel()
389+
.ConfigureServices(services =>
390+
{
391+
services.AddConnections();
392+
})
393+
.Configure(app =>
394+
{
395+
app.UseRouting();
396+
app.UseEndpoints(endpoints => configure(endpoints));
397+
})
398+
.UseUrls("http://127.0.0.1:0");
392399
})
393-
.UseUrls("http://127.0.0.1:0")
394400
.Build();
395401
}
396402

397-
private IWebHost BuildWebHost<TConnectionHandler>(string path, Action<HttpConnectionDispatcherOptions> configureOptions) where TConnectionHandler : ConnectionHandler
403+
private IHost BuildWebHost<TConnectionHandler>(string path, Action<HttpConnectionDispatcherOptions> configureOptions) where TConnectionHandler : ConnectionHandler
398404
{
399-
return new WebHostBuilder()
400-
.UseUrls("http://127.0.0.1:0")
401-
.UseKestrel()
402-
.ConfigureServices(services =>
403-
{
404-
services.AddConnections();
405-
})
406-
.Configure(app =>
405+
return new HostBuilder()
406+
.ConfigureWebHost(webHostBuilder =>
407407
{
408-
app.UseRouting();
409-
app.UseEndpoints(routes =>
408+
webHostBuilder
409+
.UseUrls("http://127.0.0.1:0")
410+
.UseKestrel()
411+
.ConfigureServices(services =>
412+
{
413+
services.AddConnections();
414+
})
415+
.Configure(app =>
416+
{
417+
app.UseRouting();
418+
app.UseEndpoints(routes =>
419+
{
420+
routes.MapConnectionHandler<TConnectionHandler>(path, configureOptions);
421+
});
422+
})
423+
.ConfigureLogging(factory =>
410424
{
411-
routes.MapConnectionHandler<TConnectionHandler>(path, configureOptions);
425+
factory.AddXunit(_output, LogLevel.Trace);
412426
});
413427
})
414-
.ConfigureLogging(factory =>
415-
{
416-
factory.AddXunit(_output, LogLevel.Trace);
417-
})
418428
.Build();
419429
}
420430
}

0 commit comments

Comments
 (0)