@@ -234,85 +234,123 @@ public void DisableDefaultIHostEnvironmentValues()
234
234
[ InlineData ( false ) ]
235
235
public void ConfigurationSettingCanInfluenceEnvironment ( bool disableDefaults )
236
236
{
237
- var tempPath = Path . GetTempPath ( ) ;
237
+ var tempPath = CreateTempSubdirectory ( ) ;
238
238
239
- using var config = new ConfigurationManager ( ) ;
240
-
241
- config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
239
+ try
242
240
{
243
- new ( HostDefaults . ApplicationKey , "AppA" ) ,
244
- new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
245
- new ( HostDefaults . ContentRootKey , tempPath )
246
- } ) ;
241
+ using var config = new ConfigurationManager ( ) ;
247
242
248
- var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
249
- {
250
- DisableDefaults = disableDefaults ,
251
- Configuration = config ,
252
- } ) ;
243
+ config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
244
+ {
245
+ new ( HostDefaults . ApplicationKey , "AppA" ) ,
246
+ new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
247
+ new ( HostDefaults . ContentRootKey , tempPath )
248
+ } ) ;
253
249
254
- Assert . Equal ( "AppA" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
255
- Assert . Equal ( "EnvA" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
256
- Assert . Equal ( tempPath , builder . Configuration [ HostDefaults . ContentRootKey ] ) ;
250
+ var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
251
+ {
252
+ DisableDefaults = disableDefaults ,
253
+ Configuration = config ,
254
+ } ) ;
257
255
258
- Assert . Equal ( "AppA" , builder . Environment . ApplicationName ) ;
259
- Assert . Equal ( "EnvA" , builder . Environment . EnvironmentName ) ;
260
- Assert . Equal ( tempPath , builder . Environment . ContentRootPath ) ;
261
- var fileProviderFromBuilder = Assert . IsType < PhysicalFileProvider > ( builder . Environment . ContentRootFileProvider ) ;
262
- Assert . Equal ( tempPath , fileProviderFromBuilder . Root ) ;
256
+ Assert . Equal ( "AppA" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
257
+ Assert . Equal ( "EnvA" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
258
+ Assert . Equal ( tempPath , builder . Configuration [ HostDefaults . ContentRootKey ] ) ;
263
259
264
- using IHost host = builder . Build ( ) ;
260
+ Assert . Equal ( "AppA" , builder . Environment . ApplicationName ) ;
261
+ Assert . Equal ( "EnvA" , builder . Environment . EnvironmentName ) ;
262
+ Assert . Equal ( tempPath , builder . Environment . ContentRootPath ) ;
263
+ var fileProviderFromBuilder = Assert . IsType < PhysicalFileProvider > ( builder . Environment . ContentRootFileProvider ) ;
264
+ Assert . Equal ( tempPath , fileProviderFromBuilder . Root ) ;
265
265
266
- var hostEnvironmentFromServices = host . Services . GetRequiredService < IHostEnvironment > ( ) ;
267
- Assert . Equal ( "AppA" , hostEnvironmentFromServices . ApplicationName ) ;
268
- Assert . Equal ( "EnvA" , hostEnvironmentFromServices . EnvironmentName ) ;
269
- Assert . Equal ( tempPath , hostEnvironmentFromServices . ContentRootPath ) ;
270
- var fileProviderFromServices = Assert . IsType < PhysicalFileProvider > ( hostEnvironmentFromServices . ContentRootFileProvider ) ;
271
- Assert . Equal ( tempPath , fileProviderFromServices . Root ) ;
266
+ using IHost host = builder . Build ( ) ;
267
+
268
+ var hostEnvironmentFromServices = host . Services . GetRequiredService < IHostEnvironment > ( ) ;
269
+ Assert . Equal ( "AppA" , hostEnvironmentFromServices . ApplicationName ) ;
270
+ Assert . Equal ( "EnvA" , hostEnvironmentFromServices . EnvironmentName ) ;
271
+ Assert . Equal ( tempPath , hostEnvironmentFromServices . ContentRootPath ) ;
272
+ var fileProviderFromServices = Assert . IsType < PhysicalFileProvider > ( hostEnvironmentFromServices . ContentRootFileProvider ) ;
273
+ Assert . Equal ( tempPath , fileProviderFromServices . Root ) ;
274
+ }
275
+ finally
276
+ {
277
+ Directory . Delete ( tempPath ) ;
278
+ }
272
279
}
273
280
274
281
[ Theory ]
275
282
[ InlineData ( true ) ]
276
283
[ InlineData ( false ) ]
277
284
public void DirectSettingsOverrideConfigurationSetting ( bool disableDefaults )
278
285
{
279
- var tempPath = Path . GetTempPath ( ) ;
280
-
281
- using var config = new ConfigurationManager ( ) ;
286
+ var tempPath = CreateTempSubdirectory ( ) ;
282
287
283
- config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
288
+ try
284
289
{
285
- new ( HostDefaults . ApplicationKey , "AppA" ) ,
286
- new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
287
- } ) ;
290
+ using var config = new ConfigurationManager ( ) ;
288
291
289
- var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
290
- {
291
- DisableDefaults = disableDefaults ,
292
- Configuration = config ,
293
- ApplicationName = "AppB" ,
294
- EnvironmentName = "EnvB" ,
295
- ContentRootPath = tempPath ,
296
- } ) ;
292
+ config . AddInMemoryCollection ( new KeyValuePair < string , string > [ ]
293
+ {
294
+ new ( HostDefaults . ApplicationKey , "AppA" ) ,
295
+ new ( HostDefaults . EnvironmentKey , "EnvA" ) ,
296
+ } ) ;
297
297
298
- Assert . Equal ( "AppB" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
299
- Assert . Equal ( "EnvB" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
300
- Assert . Equal ( tempPath , builder . Configuration [ HostDefaults . ContentRootKey ] ) ;
298
+ var builder = new HostApplicationBuilder ( new HostApplicationBuilderSettings
299
+ {
300
+ DisableDefaults = disableDefaults ,
301
+ Configuration = config ,
302
+ ApplicationName = "AppB" ,
303
+ EnvironmentName = "EnvB" ,
304
+ ContentRootPath = tempPath ,
305
+ } ) ;
301
306
302
- Assert . Equal ( "AppB" , builder . Environment . ApplicationName ) ;
303
- Assert . Equal ( "EnvB" , builder . Environment . EnvironmentName ) ;
304
- Assert . Equal ( tempPath , builder . Environment . ContentRootPath ) ;
305
- var fileProviderFromBuilder = Assert . IsType < PhysicalFileProvider > ( builder . Environment . ContentRootFileProvider ) ;
306
- Assert . Equal ( tempPath , fileProviderFromBuilder . Root ) ;
307
+ Assert . Equal ( "AppB" , builder . Configuration [ HostDefaults . ApplicationKey ] ) ;
308
+ Assert . Equal ( "EnvB" , builder . Configuration [ HostDefaults . EnvironmentKey ] ) ;
309
+ Assert . Equal ( tempPath , builder . Configuration [ HostDefaults . ContentRootKey ] ) ;
307
310
308
- using IHost host = builder . Build ( ) ;
311
+ Assert . Equal ( "AppB" , builder . Environment . ApplicationName ) ;
312
+ Assert . Equal ( "EnvB" , builder . Environment . EnvironmentName ) ;
313
+ Assert . Equal ( tempPath , builder . Environment . ContentRootPath ) ;
314
+ var fileProviderFromBuilder = Assert . IsType < PhysicalFileProvider > ( builder . Environment . ContentRootFileProvider ) ;
315
+ Assert . Equal ( tempPath , fileProviderFromBuilder . Root ) ;
309
316
310
- var hostEnvironmentFromServices = host . Services . GetRequiredService < IHostEnvironment > ( ) ;
311
- Assert . Equal ( "AppB" , hostEnvironmentFromServices . ApplicationName ) ;
312
- Assert . Equal ( "EnvB" , hostEnvironmentFromServices . EnvironmentName ) ;
313
- Assert . Equal ( tempPath , hostEnvironmentFromServices . ContentRootPath ) ;
314
- var fileProviderFromServices = Assert . IsType < PhysicalFileProvider > ( hostEnvironmentFromServices . ContentRootFileProvider ) ;
315
- Assert . Equal ( tempPath , fileProviderFromServices . Root ) ;
317
+ using IHost host = builder . Build ( ) ;
318
+
319
+ var hostEnvironmentFromServices = host . Services . GetRequiredService < IHostEnvironment > ( ) ;
320
+ Assert . Equal ( "AppB" , hostEnvironmentFromServices . ApplicationName ) ;
321
+ Assert . Equal ( "EnvB" , hostEnvironmentFromServices . EnvironmentName ) ;
322
+ Assert . Equal ( tempPath , hostEnvironmentFromServices . ContentRootPath ) ;
323
+ var fileProviderFromServices = Assert . IsType < PhysicalFileProvider > ( hostEnvironmentFromServices . ContentRootFileProvider ) ;
324
+ Assert . Equal ( tempPath , fileProviderFromServices . Root ) ;
325
+ }
326
+ finally
327
+ {
328
+ Directory . Delete ( tempPath ) ;
329
+ }
330
+ }
331
+
332
+ private static string CreateTempSubdirectory ( )
333
+ {
334
+ #if NETCOREAPP
335
+ DirectoryInfo directoryInfo = Directory . CreateTempSubdirectory ( ) ;
336
+ #else
337
+ DirectoryInfo directoryInfo = new DirectoryInfo ( Path . Combine ( Path . GetTempPath ( ) , Path . GetRandomFileName ( ) ) ) ;
338
+ directoryInfo . Create ( ) ;
339
+ #endif
340
+
341
+ // PhysicalFileProvider will always ensure the path has a trailing slash
342
+ return EnsureTrailingSlash ( directoryInfo . FullName ) ;
343
+ }
344
+
345
+ private static string EnsureTrailingSlash ( string path )
346
+ {
347
+ if ( ! string . IsNullOrEmpty ( path ) &&
348
+ path [ path . Length - 1 ] != Path . DirectorySeparatorChar )
349
+ {
350
+ return path + Path . DirectorySeparatorChar ;
351
+ }
352
+
353
+ return path ;
316
354
}
317
355
318
356
[ Fact ]
0 commit comments