3
3
4
4
using System ;
5
5
using System . IO ;
6
+ using System . Net ;
6
7
using System . Runtime ;
7
8
using System . Threading ;
8
9
using Benchmarks . Configuration ;
9
10
using Microsoft . AspNetCore . Hosting ;
10
- using Microsoft . AspNetCore . Server . Kestrel . Filter ;
11
+ using Microsoft . AspNetCore . Server . Kestrel ;
12
+ using Microsoft . AspNetCore . Server . Kestrel . Adapter ;
11
13
using Microsoft . Extensions . Configuration ;
12
14
using Microsoft . Extensions . DependencyInjection ;
13
15
@@ -48,25 +50,31 @@ public static void Main(string[] args)
48
50
49
51
if ( String . Equals ( Server , "Kestrel" , StringComparison . OrdinalIgnoreCase ) )
50
52
{
51
- var threads = GetThreadCount ( config ) ;
52
- webHostBuilder = webHostBuilder . UseKestrel ( ( options ) =>
53
+ webHostBuilder = webHostBuilder . UseKestrel ( options =>
53
54
{
54
- var connectionFilter = GetConnectionFilter ( config , options . ConnectionFilter ) ;
55
- if ( connectionFilter != null )
55
+ var urls = config [ "urls" ] ?? config [ "server.urls" ] ;
56
+
57
+ if ( ! string . IsNullOrEmpty ( urls ) )
56
58
{
57
- options . ConnectionFilter = connectionFilter ;
59
+ foreach ( var value in urls . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) )
60
+ {
61
+ Listen ( options , config , value ) ;
62
+ }
58
63
}
59
-
60
- if ( threads > 0 )
64
+ else
61
65
{
62
- options . ThreadCount = threads ;
66
+ Listen ( options , config , "http://localhost:5000/" ) ;
63
67
}
64
68
65
- if ( UrlContainsHttps ( config ) )
69
+ var threads = GetThreadCount ( config ) ;
70
+
71
+ if ( threads > 0 )
66
72
{
67
- options . UseHttps ( "testCert.pfx" , "testPassword" ) ;
73
+ options . ThreadCount = threads ;
68
74
}
69
75
} ) ;
76
+
77
+ webHostBuilder . UseSetting ( WebHostDefaults . ServerUrlsKey , string . Empty ) ;
70
78
}
71
79
else if ( String . Equals ( Server , "WebListener" , StringComparison . OrdinalIgnoreCase ) )
72
80
{
@@ -146,7 +154,7 @@ private static int GetThreadCount(IConfigurationRoot config)
146
154
return threadCountValue == null ? - 1 : int . Parse ( threadCountValue ) ;
147
155
}
148
156
149
- private static IConnectionFilter GetConnectionFilter ( IConfigurationRoot config , IConnectionFilter prevFilter )
157
+ private static IConnectionAdapter GetConnectionFilter ( IConfigurationRoot config )
150
158
{
151
159
var connectionFilterValue = config [ "connectionFilter" ] ;
152
160
if ( string . IsNullOrEmpty ( connectionFilterValue ) )
@@ -156,27 +164,27 @@ private static IConnectionFilter GetConnectionFilter(IConfigurationRoot config,
156
164
else
157
165
{
158
166
var connectionFilterType = Type . GetType ( connectionFilterValue , throwOnError : true ) ;
159
- return ( IConnectionFilter ) Activator . CreateInstance ( connectionFilterType , prevFilter ?? new NoOpConnectionFilter ( ) ) ;
167
+ return ( IConnectionAdapter ) Activator . CreateInstance ( connectionFilterType ) ;
160
168
}
161
169
}
162
170
163
- // Copied from https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs
164
- private static bool UrlContainsHttps ( IConfiguration config )
171
+ private static void Listen ( KestrelServerOptions options , IConfigurationRoot config , string url )
165
172
{
166
- var urls = config [ "urls" ] ?? config [ "server.urls" ] ;
167
-
168
- if ( ! string . IsNullOrEmpty ( urls ) )
173
+ var uri = new Uri ( url ) ;
174
+
175
+ options . Listen ( IPAddress . Loopback , uri . Port , listenOptions =>
169
176
{
170
- foreach ( var value in urls . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries ) )
177
+ var connectionFilter = GetConnectionFilter ( config ) ;
178
+ if ( connectionFilter != null )
171
179
{
172
- if ( value . StartsWith ( "https://" , StringComparison . OrdinalIgnoreCase ) ) {
173
- return true ;
174
- }
180
+ listenOptions . ConnectionAdapters . Add ( connectionFilter ) ;
175
181
}
176
- }
177
182
178
- return false ;
183
+ if ( uri . Scheme . Equals ( "https" , StringComparison . OrdinalIgnoreCase ) )
184
+ {
185
+ listenOptions . UseHttps ( "testCert.pfx" , "testPassword" ) ;
186
+ }
187
+ } ) ;
179
188
}
180
189
}
181
190
}
182
-
0 commit comments