77using System . IO ;
88using System . Linq ;
99using System . Net ;
10+ using System . Threading ;
1011using System . Threading . Tasks ;
1112using Microsoft . AspNetCore . Builder ;
1213using Microsoft . AspNetCore . Connections ;
@@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
2021{
2122 internal class AddressBinder
2223 {
23- public static async Task BindAsync ( IEnumerable < ListenOptions > listenOptions , AddressBindContext context )
24+ public static async Task BindAsync ( IEnumerable < ListenOptions > listenOptions , AddressBindContext context , CancellationToken cancellationToken )
2425 {
2526 var strategy = CreateStrategy (
2627 listenOptions . ToArray ( ) ,
@@ -32,7 +33,7 @@ public static async Task BindAsync(IEnumerable<ListenOptions> listenOptions, Add
3233 context . ServerOptions . OptionsInUse . Clear ( ) ;
3334 context . Addresses . Clear ( ) ;
3435
35- await strategy . BindAsync ( context ) . ConfigureAwait ( false ) ;
36+ await strategy . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
3637 }
3738
3839 private static IStrategy CreateStrategy ( ListenOptions [ ] listenOptions , string [ ] addresses , bool preferAddresses )
@@ -86,11 +87,11 @@ protected internal static bool TryCreateIPEndPoint(BindingAddress address, [NotN
8687 return true ;
8788 }
8889
89- internal static async Task BindEndpointAsync ( ListenOptions endpoint , AddressBindContext context )
90+ internal static async Task BindEndpointAsync ( ListenOptions endpoint , AddressBindContext context , CancellationToken cancellationToken )
9091 {
9192 try
9293 {
93- await context . CreateBinding ( endpoint ) . ConfigureAwait ( false ) ;
94+ await context . CreateBinding ( endpoint , cancellationToken ) . ConfigureAwait ( false ) ;
9495 }
9596 catch ( AddressInUseException ex )
9697 {
@@ -144,24 +145,24 @@ internal static ListenOptions ParseAddress(string address, out bool https)
144145
145146 private interface IStrategy
146147 {
147- Task BindAsync ( AddressBindContext context ) ;
148+ Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken ) ;
148149 }
149150
150151 private class DefaultAddressStrategy : IStrategy
151152 {
152- public async Task BindAsync ( AddressBindContext context )
153+ public async Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
153154 {
154155 var httpDefault = ParseAddress ( Constants . DefaultServerAddress , out _ ) ;
155156 context . ServerOptions . ApplyEndpointDefaults ( httpDefault ) ;
156- await httpDefault . BindAsync ( context ) . ConfigureAwait ( false ) ;
157+ await httpDefault . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
157158
158159 // Conditional https default, only if a cert is available
159160 var httpsDefault = ParseAddress ( Constants . DefaultServerHttpsAddress , out _ ) ;
160161 context . ServerOptions . ApplyEndpointDefaults ( httpsDefault ) ;
161162
162163 if ( httpsDefault . IsTls || httpsDefault . TryUseHttps ( ) )
163164 {
164- await httpsDefault . BindAsync ( context ) . ConfigureAwait ( false ) ;
165+ await httpsDefault . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
165166 context . Logger . LogDebug ( CoreStrings . BindingToDefaultAddresses ,
166167 Constants . DefaultServerAddress , Constants . DefaultServerHttpsAddress ) ;
167168 }
@@ -180,12 +181,12 @@ public OverrideWithAddressesStrategy(IReadOnlyCollection<string> addresses)
180181 {
181182 }
182183
183- public override Task BindAsync ( AddressBindContext context )
184+ public override Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
184185 {
185186 var joined = string . Join ( ", " , _addresses ) ;
186187 context . Logger . LogInformation ( CoreStrings . OverridingWithPreferHostingUrls , nameof ( IServerAddressesFeature . PreferHostingUrls ) , joined ) ;
187188
188- return base . BindAsync ( context ) ;
189+ return base . BindAsync ( context , cancellationToken ) ;
189190 }
190191 }
191192
@@ -199,12 +200,12 @@ public OverrideWithEndpointsStrategy(IReadOnlyCollection<ListenOptions> endpoint
199200 _originalAddresses = originalAddresses ;
200201 }
201202
202- public override Task BindAsync ( AddressBindContext context )
203+ public override Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
203204 {
204205 var joined = string . Join ( ", " , _originalAddresses ) ;
205206 context . Logger . LogWarning ( CoreStrings . OverridingWithKestrelOptions , joined ) ;
206207
207- return base . BindAsync ( context ) ;
208+ return base . BindAsync ( context , cancellationToken ) ;
208209 }
209210 }
210211
@@ -217,11 +218,11 @@ public EndpointsStrategy(IReadOnlyCollection<ListenOptions> endpoints)
217218 _endpoints = endpoints ;
218219 }
219220
220- public virtual async Task BindAsync ( AddressBindContext context )
221+ public virtual async Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
221222 {
222223 foreach ( var endpoint in _endpoints )
223224 {
224- await endpoint . BindAsync ( context ) . ConfigureAwait ( false ) ;
225+ await endpoint . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
225226 }
226227 }
227228 }
@@ -235,7 +236,7 @@ public AddressesStrategy(IReadOnlyCollection<string> addresses)
235236 _addresses = addresses ;
236237 }
237238
238- public virtual async Task BindAsync ( AddressBindContext context )
239+ public virtual async Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
239240 {
240241 foreach ( var address in _addresses )
241242 {
@@ -247,7 +248,7 @@ public virtual async Task BindAsync(AddressBindContext context)
247248 options . UseHttps ( ) ;
248249 }
249250
250- await options . BindAsync ( context ) . ConfigureAwait ( false ) ;
251+ await options . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
251252 }
252253 }
253254 }
0 commit comments