44using  System . Security . Cryptography . X509Certificates ; 
55using  Microsoft . AspNetCore . Connections ; 
66using  Microsoft . AspNetCore . Hosting ; 
7+ using  Microsoft . AspNetCore . Hosting . Server ; 
78using  Microsoft . AspNetCore . Hosting . Server . Features ; 
89using  Microsoft . AspNetCore . Server . Kestrel . Core ; 
910using  Microsoft . AspNetCore . Server . Kestrel . Https ; 
1011using  Microsoft . Extensions . Configuration ; 
1112using  Microsoft . Extensions . DependencyInjection ; 
13+ using  Microsoft . Extensions . Hosting ; 
1214
1315namespace  Microsoft . AspNetCore . Server . Kestrel . Tests ; 
1416
@@ -21,34 +23,37 @@ public class HttpsConfigurationTests
2123    [ InlineData ( "https://127.0.0.1:0" ,  false ) ] 
2224    public  async  Task  BindAddressFromSetting ( string  address ,  bool  useKestrelHttpsConfiguration ) 
2325    { 
24-         var  hostBuilder  =  new  WebHostBuilder ( ) 
25-                 . UseKestrelCore ( ) 
26-                 . ConfigureKestrel ( serverOptions => 
27-                 { 
28-                     serverOptions . TestOverrideDefaultCertificate  =  new  X509Certificate2 ( Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ,  "testPassword" ) ; 
29-                 } ) 
30-                 . Configure ( app =>  {  } ) ; 
31- 
32-         // This is what ASPNETCORE_URLS would populate 
33-         hostBuilder . UseSetting ( WebHostDefaults . ServerUrlsKey ,  address ) ; 
26+         var  hostBuilder  =  new  HostBuilder ( ) 
27+             . ConfigureWebHost ( webHostBuilder => 
28+             { 
29+                 webHostBuilder 
30+                     . UseKestrelCore ( ) 
31+                     . ConfigureKestrel ( serverOptions => 
32+                     { 
33+                         serverOptions . TestOverrideDefaultCertificate  =  new  X509Certificate2 ( Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ,  "testPassword" ) ; 
34+                     } ) 
35+                     . Configure ( app =>  {  } ) 
36+                     // This is what ASPNETCORE_URLS would populate 
37+                     . UseSetting ( WebHostDefaults . ServerUrlsKey ,  address ) ; 
3438
35-         if  ( useKestrelHttpsConfiguration ) 
36-         { 
37-             hostBuilder . UseKestrelHttpsConfiguration ( ) ; 
38-         } 
39+                 if  ( useKestrelHttpsConfiguration ) 
40+                 { 
41+                     webHostBuilder . UseKestrelHttpsConfiguration ( ) ; 
42+                 } 
43+             } ) ; 
3944
4045        var  host  =  hostBuilder . Build ( ) ; 
46+         await  host . StartAsync ( ) ; 
4147
42-         Assert . Single ( host . ServerFeatures . Get < IServerAddressesFeature > ( ) . Addresses ,  address ) ; 
48+         Assert . Single ( host . Services . GetRequiredService < IServer > ( ) . Features . Get < IServerAddressesFeature > ( ) . Addresses ,  address ) ; 
4349
4450        if  ( address . StartsWith ( "https" ,  StringComparison . OrdinalIgnoreCase )  &&  ! useKestrelHttpsConfiguration ) 
4551        { 
4652            Assert . Throws < InvalidOperationException > ( host . Run ) ; 
4753        } 
4854        else 
4955        { 
50-             // Binding succeeds 
51-             await  host . StartAsync ( ) ; 
56+             // Binding succeeds - server is already started, so we just stop it 
5257            await  host . StopAsync ( ) ; 
5358        } 
5459    } 
@@ -59,16 +64,20 @@ public void NoFallbackToHttpAddress()
5964        const  string  httpAddress  =  "http://127.0.0.1:0" ; 
6065        const  string  httpsAddress  =  "https://localhost:5001" ; 
6166
62-         var  hostBuilder  =  new  WebHostBuilder ( ) 
63-                 . UseKestrelCore ( ) 
64-                 . Configure ( app =>  {  } ) ; 
65- 
66-         // This is what ASPNETCORE_URLS would populate 
67-         hostBuilder . UseSetting ( WebHostDefaults . ServerUrlsKey ,  $ "{ httpAddress } ;{ httpsAddress } ") ; 
67+         var  hostBuilder  =  new  HostBuilder ( ) 
68+             . ConfigureWebHost ( webHostBuilder => 
69+             { 
70+                 webHostBuilder 
71+                     . UseKestrelCore ( ) 
72+                     . Configure ( app =>  {  } ) 
73+                     // This is what ASPNETCORE_URLS would populate 
74+                     . UseSetting ( WebHostDefaults . ServerUrlsKey ,  $ "{ httpAddress } ;{ httpsAddress } ") ; 
75+             } ) ; 
6876
6977        var  host  =  hostBuilder . Build ( ) ; 
78+         await  host . StartAsync ( ) ; 
7079
71-         Assert . Equal ( new [ ]  {  httpAddress ,  httpsAddress  } ,  host . ServerFeatures . Get < IServerAddressesFeature > ( ) . Addresses ) ; 
80+         Assert . Equal ( new [ ]  {  httpAddress ,  httpsAddress  } ,  host . Services . GetRequiredService < IServer > ( ) . Features . Get < IServerAddressesFeature > ( ) . Addresses ) ; 
7281
7382        Assert . Throws < InvalidOperationException > ( host . Run ) ; 
7483    } 
@@ -80,24 +89,28 @@ public void NoFallbackToHttpAddress()
8089    [ InlineData ( "https://127.0.0.1:0" ,  false ) ] 
8190    public  async  Task  BindAddressFromEndpoint ( string  address ,  bool  useKestrelHttpsConfiguration ) 
8291    { 
83-         var  hostBuilder  =  new  WebHostBuilder ( ) 
84-                 . UseKestrelCore ( ) 
85-                 . ConfigureKestrel ( serverOptions => 
86-                 { 
87-                     var  config  =  new  ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ] 
92+         var  hostBuilder  =  new  HostBuilder ( ) 
93+             . ConfigureWebHost ( webHostBuilder => 
94+             { 
95+                 webHostBuilder 
96+                     . UseKestrelCore ( ) 
97+                     . ConfigureKestrel ( serverOptions => 
8898                    { 
89-                         new  KeyValuePair < string ,  string > ( "Endpoints:end1:Url" ,  address ) , 
90-                         new  KeyValuePair < string ,  string > ( "Certificates:Default:Path" ,  Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ) , 
91-                         new  KeyValuePair < string ,  string > ( "Certificates:Default:Password" ,  "testPassword" ) , 
92-                     } ) . Build ( ) ; 
93-                     serverOptions . Configure ( config ) ; 
94-                 } ) 
95-                 . Configure ( app =>  {  } ) ; 
96- 
97-         if  ( useKestrelHttpsConfiguration ) 
98-         { 
99-             hostBuilder . UseKestrelHttpsConfiguration ( ) ; 
100-         } 
99+                         var  config  =  new  ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ] 
100+                         { 
101+                             new  KeyValuePair < string ,  string > ( "Endpoints:end1:Url" ,  address ) , 
102+                             new  KeyValuePair < string ,  string > ( "Certificates:Default:Path" ,  Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ) , 
103+                             new  KeyValuePair < string ,  string > ( "Certificates:Default:Password" ,  "testPassword" ) , 
104+                         } ) . Build ( ) ; 
105+                         serverOptions . Configure ( config ) ; 
106+                     } ) 
107+                     . Configure ( app =>  {  } ) ; 
108+ 
109+                 if  ( useKestrelHttpsConfiguration ) 
110+                 { 
111+                     webHostBuilder . UseKestrelHttpsConfiguration ( ) ; 
112+                 } 
113+             } ) ; 
101114
102115        var  host  =  hostBuilder . Build ( ) ; 
103116
@@ -118,23 +131,27 @@ public async Task BindAddressFromEndpoint(string address, bool useKestrelHttpsCo
118131    [ InlineData ( false ) ] 
119132    public  async  Task  LoadDefaultCertificate ( bool  useKestrelHttpsConfiguration ) 
120133    { 
121-         var  hostBuilder  =  new  WebHostBuilder ( ) 
122-                 . UseKestrelCore ( ) 
123-                 . ConfigureKestrel ( serverOptions => 
124-                 { 
125-                     var  config  =  new  ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ] 
134+         var  hostBuilder  =  new  HostBuilder ( ) 
135+             . ConfigureWebHost ( webHostBuilder => 
136+             { 
137+                 webHostBuilder 
138+                     . UseKestrelCore ( ) 
139+                     . ConfigureKestrel ( serverOptions => 
126140                    { 
127-                         new  KeyValuePair < string ,  string > ( "Certificates:Default:Path" ,  Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ) , 
128-                         new  KeyValuePair < string ,  string > ( "Certificates:Default:Password" ,  "testPassword" ) , 
129-                     } ) . Build ( ) ; 
130-                     serverOptions . Configure ( config ) ; 
131-                 } ) 
132-                 . Configure ( app =>  {  } ) ; 
133- 
134-         if  ( useKestrelHttpsConfiguration ) 
135-         { 
136-             hostBuilder . UseKestrelHttpsConfiguration ( ) ; 
137-         } 
141+                         var  config  =  new  ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ] 
142+                         { 
143+                             new  KeyValuePair < string ,  string > ( "Certificates:Default:Path" ,  Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ) , 
144+                             new  KeyValuePair < string ,  string > ( "Certificates:Default:Password" ,  "testPassword" ) , 
145+                         } ) . Build ( ) ; 
146+                         serverOptions . Configure ( config ) ; 
147+                     } ) 
148+                     . Configure ( app =>  {  } ) ; 
149+ 
150+                 if  ( useKestrelHttpsConfiguration ) 
151+                 { 
152+                     webHostBuilder . UseKestrelHttpsConfiguration ( ) ; 
153+                 } 
154+             } ) ; 
138155
139156        var  host  =  hostBuilder . Build ( ) ; 
140157
@@ -150,24 +167,28 @@ public async Task LoadDefaultCertificate(bool useKestrelHttpsConfiguration)
150167    [ InlineData ( "https://127.0.0.1:0" ,  false ) ] 
151168    public  async  Task  LoadEndpointCertificate ( string  address ,  bool  useKestrelHttpsConfiguration ) 
152169    { 
153-         var  hostBuilder  =  new  WebHostBuilder ( ) 
154-                 . UseKestrelCore ( ) 
155-                 . ConfigureKestrel ( serverOptions => 
156-                 { 
157-                     var  config  =  new  ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ] 
170+         var  hostBuilder  =  new  HostBuilder ( ) 
171+             . ConfigureWebHost ( webHostBuilder => 
172+             { 
173+                 webHostBuilder 
174+                     . UseKestrelCore ( ) 
175+                     . ConfigureKestrel ( serverOptions => 
158176                    { 
159-                         new  KeyValuePair < string ,  string > ( "Endpoints:end1:Url" ,  address ) , 
160-                         new  KeyValuePair < string ,  string > ( "Certificates:Default:Path" ,  Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ) , 
161-                         new  KeyValuePair < string ,  string > ( "Certificates:Default:Password" ,  "testPassword" ) , 
162-                     } ) . Build ( ) ; 
163-                     serverOptions . Configure ( config ) ; 
164-                 } ) 
165-                 . Configure ( app =>  {  } ) ; 
166- 
167-         if  ( useKestrelHttpsConfiguration ) 
168-         { 
169-             hostBuilder . UseKestrelHttpsConfiguration ( ) ; 
170-         } 
177+                         var  config  =  new  ConfigurationBuilder ( ) . AddInMemoryCollection ( new [ ] 
178+                         { 
179+                             new  KeyValuePair < string ,  string > ( "Endpoints:end1:Url" ,  address ) , 
180+                             new  KeyValuePair < string ,  string > ( "Certificates:Default:Path" ,  Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ) , 
181+                             new  KeyValuePair < string ,  string > ( "Certificates:Default:Password" ,  "testPassword" ) , 
182+                         } ) . Build ( ) ; 
183+                         serverOptions . Configure ( config ) ; 
184+                     } ) 
185+                     . Configure ( app =>  {  } ) ; 
186+ 
187+                 if  ( useKestrelHttpsConfiguration ) 
188+                 { 
189+                     webHostBuilder . UseKestrelHttpsConfiguration ( ) ; 
190+                 } 
191+             } ) ; 
171192
172193        var  host  =  hostBuilder . Build ( ) ; 
173194
@@ -186,18 +207,22 @@ public async Task LoadEndpointCertificate(string address, bool useKestrelHttpsCo
186207    [ Fact ] 
187208    public  async  Task  UseHttpsJustWorks ( ) 
188209    { 
189-         var  hostBuilder  =  new  WebHostBuilder ( ) 
190-             . UseKestrelCore ( ) 
191-             . ConfigureKestrel ( serverOptions => 
210+         var  hostBuilder  =  new  HostBuilder ( ) 
211+             . ConfigureWebHost ( webHostBuilder => 
192212            { 
193-                 serverOptions . TestOverrideDefaultCertificate  =  new  X509Certificate2 ( Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ,  "testPassword" ) ; 
213+                 webHostBuilder 
214+                     . UseKestrelCore ( ) 
215+                     . ConfigureKestrel ( serverOptions => 
216+                     { 
217+                         serverOptions . TestOverrideDefaultCertificate  =  new  X509Certificate2 ( Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ,  "testPassword" ) ; 
194218
195-                 serverOptions . ListenAnyIP ( 0 ,  listenOptions => 
196-                 { 
197-                     listenOptions . UseHttps ( ) ; 
198-                 } ) ; 
199-             } ) 
200-             . Configure ( app =>  {  } ) ; 
219+                         serverOptions . ListenAnyIP ( 0 ,  listenOptions => 
220+                         { 
221+                             listenOptions . UseHttps ( ) ; 
222+                         } ) ; 
223+                     } ) 
224+                     . Configure ( app =>  {  } ) ; 
225+             } ) ; 
201226
202227        var  host  =  hostBuilder . Build ( ) ; 
203228
@@ -211,19 +236,23 @@ public async Task UseHttpsJustWorks()
211236    [ Fact ] 
212237    public  async  Task  UseHttpsMayNotImplyUseKestrelHttpsConfiguration ( ) 
213238    { 
214-         var  hostBuilder  =  new  WebHostBuilder ( ) 
215-             . UseKestrelCore ( ) 
216-             . ConfigureKestrel ( serverOptions => 
239+         var  hostBuilder  =  new  HostBuilder ( ) 
240+             . ConfigureWebHost ( webHostBuilder => 
217241            { 
218-                 serverOptions . ListenAnyIP ( 0 ,  listenOptions  => 
219-                 { 
220-                     listenOptions . UseHttps ( new   HttpsConnectionAdapterOptions ( ) 
242+                 webHostBuilder 
243+                      . UseKestrelCore ( ) 
244+                     . ConfigureKestrel ( serverOptions  => 
221245                    { 
222-                         ServerCertificate  =  new  X509Certificate2 ( Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ,  "testPassword" ) , 
223-                     } ) ; 
224-                 } ) ; 
225-             } ) 
226-             . Configure ( app =>  {  } ) ; 
246+                         serverOptions . ListenAnyIP ( 0 ,  listenOptions => 
247+                         { 
248+                             listenOptions . UseHttps ( new  HttpsConnectionAdapterOptions ( ) 
249+                             { 
250+                                 ServerCertificate  =  new  X509Certificate2 ( Path . Combine ( "shared" ,  "TestCertificates" ,  "aspnetdevcert.pfx" ) ,  "testPassword" ) , 
251+                             } ) ; 
252+                         } ) ; 
253+                     } ) 
254+                     . Configure ( app =>  {  } ) ; 
255+             } ) ; 
227256
228257        var  host  =  hostBuilder . Build ( ) ; 
229258
0 commit comments