7
7
using System . IO ;
8
8
using System . Linq ;
9
9
using System . Net ;
10
+ using System . Threading ;
10
11
using System . Threading . Tasks ;
11
12
using Microsoft . AspNetCore . Builder ;
12
13
using Microsoft . AspNetCore . Connections ;
@@ -20,7 +21,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal
20
21
{
21
22
internal class AddressBinder
22
23
{
23
- public static async Task BindAsync ( IEnumerable < ListenOptions > listenOptions , AddressBindContext context )
24
+ public static async Task BindAsync ( IEnumerable < ListenOptions > listenOptions , AddressBindContext context , CancellationToken cancellationToken )
24
25
{
25
26
var strategy = CreateStrategy (
26
27
listenOptions . ToArray ( ) ,
@@ -32,7 +33,7 @@ public static async Task BindAsync(IEnumerable<ListenOptions> listenOptions, Add
32
33
context . ServerOptions . OptionsInUse . Clear ( ) ;
33
34
context . Addresses . Clear ( ) ;
34
35
35
- await strategy . BindAsync ( context ) . ConfigureAwait ( false ) ;
36
+ await strategy . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
36
37
}
37
38
38
39
private static IStrategy CreateStrategy ( ListenOptions [ ] listenOptions , string [ ] addresses , bool preferAddresses )
@@ -86,11 +87,11 @@ protected internal static bool TryCreateIPEndPoint(BindingAddress address, [NotN
86
87
return true ;
87
88
}
88
89
89
- internal static async Task BindEndpointAsync ( ListenOptions endpoint , AddressBindContext context )
90
+ internal static async Task BindEndpointAsync ( ListenOptions endpoint , AddressBindContext context , CancellationToken cancellationToken )
90
91
{
91
92
try
92
93
{
93
- await context . CreateBinding ( endpoint ) . ConfigureAwait ( false ) ;
94
+ await context . CreateBinding ( endpoint , cancellationToken ) . ConfigureAwait ( false ) ;
94
95
}
95
96
catch ( AddressInUseException ex )
96
97
{
@@ -144,24 +145,24 @@ internal static ListenOptions ParseAddress(string address, out bool https)
144
145
145
146
private interface IStrategy
146
147
{
147
- Task BindAsync ( AddressBindContext context ) ;
148
+ Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken ) ;
148
149
}
149
150
150
151
private class DefaultAddressStrategy : IStrategy
151
152
{
152
- public async Task BindAsync ( AddressBindContext context )
153
+ public async Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
153
154
{
154
155
var httpDefault = ParseAddress ( Constants . DefaultServerAddress , out _ ) ;
155
156
context . ServerOptions . ApplyEndpointDefaults ( httpDefault ) ;
156
- await httpDefault . BindAsync ( context ) . ConfigureAwait ( false ) ;
157
+ await httpDefault . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
157
158
158
159
// Conditional https default, only if a cert is available
159
160
var httpsDefault = ParseAddress ( Constants . DefaultServerHttpsAddress , out _ ) ;
160
161
context . ServerOptions . ApplyEndpointDefaults ( httpsDefault ) ;
161
162
162
163
if ( httpsDefault . IsTls || httpsDefault . TryUseHttps ( ) )
163
164
{
164
- await httpsDefault . BindAsync ( context ) . ConfigureAwait ( false ) ;
165
+ await httpsDefault . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
165
166
context . Logger . LogDebug ( CoreStrings . BindingToDefaultAddresses ,
166
167
Constants . DefaultServerAddress , Constants . DefaultServerHttpsAddress ) ;
167
168
}
@@ -180,12 +181,12 @@ public OverrideWithAddressesStrategy(IReadOnlyCollection<string> addresses)
180
181
{
181
182
}
182
183
183
- public override Task BindAsync ( AddressBindContext context )
184
+ public override Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
184
185
{
185
186
var joined = string . Join ( ", " , _addresses ) ;
186
187
context . Logger . LogInformation ( CoreStrings . OverridingWithPreferHostingUrls , nameof ( IServerAddressesFeature . PreferHostingUrls ) , joined ) ;
187
188
188
- return base . BindAsync ( context ) ;
189
+ return base . BindAsync ( context , cancellationToken ) ;
189
190
}
190
191
}
191
192
@@ -199,12 +200,12 @@ public OverrideWithEndpointsStrategy(IReadOnlyCollection<ListenOptions> endpoint
199
200
_originalAddresses = originalAddresses ;
200
201
}
201
202
202
- public override Task BindAsync ( AddressBindContext context )
203
+ public override Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
203
204
{
204
205
var joined = string . Join ( ", " , _originalAddresses ) ;
205
206
context . Logger . LogWarning ( CoreStrings . OverridingWithKestrelOptions , joined ) ;
206
207
207
- return base . BindAsync ( context ) ;
208
+ return base . BindAsync ( context , cancellationToken ) ;
208
209
}
209
210
}
210
211
@@ -217,11 +218,11 @@ public EndpointsStrategy(IReadOnlyCollection<ListenOptions> endpoints)
217
218
_endpoints = endpoints ;
218
219
}
219
220
220
- public virtual async Task BindAsync ( AddressBindContext context )
221
+ public virtual async Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
221
222
{
222
223
foreach ( var endpoint in _endpoints )
223
224
{
224
- await endpoint . BindAsync ( context ) . ConfigureAwait ( false ) ;
225
+ await endpoint . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
225
226
}
226
227
}
227
228
}
@@ -235,7 +236,7 @@ public AddressesStrategy(IReadOnlyCollection<string> addresses)
235
236
_addresses = addresses ;
236
237
}
237
238
238
- public virtual async Task BindAsync ( AddressBindContext context )
239
+ public virtual async Task BindAsync ( AddressBindContext context , CancellationToken cancellationToken )
239
240
{
240
241
foreach ( var address in _addresses )
241
242
{
@@ -247,7 +248,7 @@ public virtual async Task BindAsync(AddressBindContext context)
247
248
options . UseHttps ( ) ;
248
249
}
249
250
250
- await options . BindAsync ( context ) . ConfigureAwait ( false ) ;
251
+ await options . BindAsync ( context , cancellationToken ) . ConfigureAwait ( false ) ;
251
252
}
252
253
}
253
254
}
0 commit comments