1
1
using System ;
2
2
using System . IO ;
3
- using System . Net ;
4
3
using System . Security . Authentication ;
5
4
using Microsoft . AspNetCore . Connections ;
6
5
using Microsoft . AspNetCore . Connections . Features ;
7
6
using Microsoft . AspNetCore . Hosting ;
8
7
using Microsoft . AspNetCore . Server . Kestrel . Core ;
9
8
using Microsoft . Extensions . Configuration ;
9
+ using Microsoft . Extensions . Hosting ;
10
10
using Microsoft . Extensions . Logging ;
11
11
12
12
namespace Http2SampleApp
@@ -15,54 +15,58 @@ public class Program
15
15
{
16
16
public static void Main ( string [ ] args )
17
17
{
18
- var hostBuilder = new WebHostBuilder ( )
19
- . ConfigureLogging ( ( _ , factory ) =>
18
+ var hostBuilder = new HostBuilder ( )
19
+ . ConfigureWebHost ( webHostBuilder =>
20
20
{
21
- // Set logging to the MAX.
22
- factory . SetMinimumLevel ( LogLevel . Trace ) ;
23
- factory . AddConsole ( ) ;
24
- } )
25
- . UseKestrel ( )
26
- . ConfigureKestrel ( ( context , options ) =>
27
- {
28
- var basePort = context . Configuration . GetValue < int ? > ( "BASE_PORT" ) ?? 5000 ;
29
-
30
- // Http/1.1 endpoint for comparison
31
- options . ListenAnyIP ( basePort , listenOptions =>
32
- {
33
- listenOptions . Protocols = HttpProtocols . Http1 ;
34
- } ) ;
35
-
36
- // TLS Http/1.1 or HTTP/2 endpoint negotiated via ALPN
37
- options . ListenAnyIP ( basePort + 1 , listenOptions =>
38
- {
39
- listenOptions . Protocols = HttpProtocols . Http1AndHttp2 ;
40
- listenOptions . UseHttps ( ) ;
41
- listenOptions . Use ( ( context , next ) =>
21
+ webHostBuilder
22
+ . UseKestrel ( )
23
+ . ConfigureKestrel ( ( context , options ) =>
42
24
{
43
- // https://tools.ietf.org/html/rfc7540#appendix-A
44
- // Allows filtering TLS handshakes on a per connection basis
25
+ var basePort = context . Configuration . GetValue < int ? > ( "BASE_PORT" ) ?? 5000 ;
45
26
46
- var tlsFeature = context . Features . Get < ITlsHandshakeFeature > ( ) ;
27
+ // Http/1.1 endpoint for comparison
28
+ options . ListenAnyIP ( basePort , listenOptions =>
29
+ {
30
+ listenOptions . Protocols = HttpProtocols . Http1 ;
31
+ } ) ;
47
32
48
- if ( tlsFeature . CipherAlgorithm == CipherAlgorithmType . Null )
33
+ // TLS Http/1.1 or HTTP/2 endpoint negotiated via ALPN
34
+ options . ListenAnyIP ( basePort + 1 , listenOptions =>
49
35
{
50
- throw new NotSupportedException ( "Prohibited cipher: " + tlsFeature . CipherAlgorithm ) ;
51
- }
36
+ listenOptions . Protocols = HttpProtocols . Http1AndHttp2 ;
37
+ listenOptions . UseHttps ( ) ;
38
+ listenOptions . Use ( ( context , next ) =>
39
+ {
40
+ // https://tools.ietf.org/html/rfc7540#appendix-A
41
+ // Allows filtering TLS handshakes on a per connection basis
52
42
53
- return next ( ) ;
54
- } ) ;
55
- } ) ;
43
+ var tlsFeature = context . Features . Get < ITlsHandshakeFeature > ( ) ;
56
44
57
- // Prior knowledge, no TLS handshake. WARNING: Not supported by browsers
58
- // but useful for the h2spec tests
59
- options . ListenAnyIP ( basePort + 5 , listenOptions =>
60
- {
61
- listenOptions . Protocols = HttpProtocols . Http2 ;
62
- } ) ;
45
+ if ( tlsFeature . CipherAlgorithm == CipherAlgorithmType . Null )
46
+ {
47
+ throw new NotSupportedException ( "Prohibited cipher: " + tlsFeature . CipherAlgorithm ) ;
48
+ }
49
+
50
+ return next ( ) ;
51
+ } ) ;
52
+ } ) ;
53
+
54
+ // Prior knowledge, no TLS handshake. WARNING: Not supported by browsers
55
+ // but useful for the h2spec tests
56
+ options . ListenAnyIP ( basePort + 5 , listenOptions =>
57
+ {
58
+ listenOptions . Protocols = HttpProtocols . Http2 ;
59
+ } ) ;
60
+ } )
61
+ . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
62
+ . UseStartup < Startup > ( ) ;
63
63
} )
64
- . UseContentRoot ( Directory . GetCurrentDirectory ( ) )
65
- . UseStartup < Startup > ( ) ;
64
+ . ConfigureLogging ( ( _ , factory ) =>
65
+ {
66
+ // Set logging to the MAX.
67
+ factory . SetMinimumLevel ( LogLevel . Trace ) ;
68
+ factory . AddConsole ( ) ;
69
+ } ) ;
66
70
67
71
hostBuilder . Build ( ) . Run ( ) ;
68
72
}
0 commit comments