Skip to content

Commit fb104e3

Browse files
authored
Do not override content root with default (#79242)
1 parent ee4120e commit fb104e3

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

src/libraries/Microsoft.Extensions.Hosting/src/HostApplicationBuilder.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ public HostApplicationBuilder(HostApplicationBuilderSettings? settings)
8888

8989
if (!settings.DisableDefaults)
9090
{
91-
HostingHostBuilderExtensions.ApplyDefaultHostConfiguration(Configuration, settings.Args);
91+
if (settings.ContentRootPath is null && Configuration[HostDefaults.ContentRootKey] is null)
92+
{
93+
HostingHostBuilderExtensions.SetDefaultContentRoot(Configuration);
94+
}
95+
96+
HostingHostBuilderExtensions.AddDefaultHostConfigurationSources(Configuration, settings.Args);
9297
}
9398

9499
// HostApplicationBuilderSettings override all other config sources.

src/libraries/Microsoft.Extensions.Hosting/src/HostingHostBuilderExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ public static IHostBuilder ConfigureDefaults(this IHostBuilder builder, string[]
201201
.UseServiceProviderFactory(context => new DefaultServiceProviderFactory(CreateDefaultServiceProviderOptions(context)));
202202
}
203203

204-
internal static void ApplyDefaultHostConfiguration(IConfigurationBuilder hostConfigBuilder, string[]? args)
204+
private static void ApplyDefaultHostConfiguration(IConfigurationBuilder hostConfigBuilder, string[]? args)
205+
{
206+
SetDefaultContentRoot(hostConfigBuilder);
207+
AddDefaultHostConfigurationSources(hostConfigBuilder, args);
208+
}
209+
210+
internal static void SetDefaultContentRoot(IConfigurationBuilder hostConfigBuilder)
205211
{
206212
// If we're running anywhere other than C:\Windows\system32, we default to using the CWD for the ContentRoot.
207213
// However, since many things like Windows services and MSIX installers have C:\Windows\system32 as there CWD which is not likely
@@ -219,7 +225,10 @@ internal static void ApplyDefaultHostConfiguration(IConfigurationBuilder hostCon
219225
new KeyValuePair<string, string?>(HostDefaults.ContentRootKey, cwd),
220226
});
221227
}
228+
}
222229

230+
internal static void AddDefaultHostConfigurationSources(IConfigurationBuilder hostConfigBuilder, string[]? args)
231+
{
223232
hostConfigBuilder.AddEnvironmentVariables(prefix: "DOTNET_");
224233
if (args is { Length: > 0 })
225234
{

src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostApplicationBuilderTests.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,39 +229,55 @@ public void DisableDefaultIHostEnvironmentValues()
229229
Assert.IsAssignableFrom<PhysicalFileProvider>(env.ContentRootFileProvider);
230230
}
231231

232-
[Fact]
233-
public void ConfigurationSettingCanInfluenceEnvironment()
232+
[Theory]
233+
[InlineData(true)]
234+
[InlineData(false)]
235+
public void ConfigurationSettingCanInfluenceEnvironment(bool disableDefaults)
234236
{
237+
var tempPath = Path.GetTempPath();
238+
235239
using var config = new ConfigurationManager();
236240

237241
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
238242
{
239243
new(HostDefaults.ApplicationKey, "AppA" ),
240244
new(HostDefaults.EnvironmentKey, "EnvA" ),
245+
new(HostDefaults.ContentRootKey, tempPath)
241246
});
242247

243248
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
244249
{
245-
DisableDefaults = true,
250+
DisableDefaults = disableDefaults,
246251
Configuration = config,
247252
});
248253

249254
Assert.Equal("AppA", builder.Configuration[HostDefaults.ApplicationKey]);
250255
Assert.Equal("EnvA", builder.Configuration[HostDefaults.EnvironmentKey]);
256+
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
251257

252258
Assert.Equal("AppA", builder.Environment.ApplicationName);
253259
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);
254263

255264
using IHost host = builder.Build();
256265

257266
var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
258267
Assert.Equal("AppA", hostEnvironmentFromServices.ApplicationName);
259268
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);
260272
}
261273

262-
[Fact]
263-
public void DirectSettingsOverrideConfigurationSetting()
274+
[Theory]
275+
[InlineData(true)]
276+
[InlineData(false)]
277+
public void DirectSettingsOverrideConfigurationSetting(bool disableDefaults)
264278
{
279+
var tempPath = Path.GetTempPath();
280+
265281
using var config = new ConfigurationManager();
266282

267283
config.AddInMemoryCollection(new KeyValuePair<string, string>[]
@@ -272,23 +288,31 @@ public void DirectSettingsOverrideConfigurationSetting()
272288

273289
var builder = new HostApplicationBuilder(new HostApplicationBuilderSettings
274290
{
275-
DisableDefaults = true,
291+
DisableDefaults = disableDefaults,
276292
Configuration = config,
277293
ApplicationName = "AppB",
278294
EnvironmentName = "EnvB",
295+
ContentRootPath = tempPath,
279296
});
280297

281298
Assert.Equal("AppB", builder.Configuration[HostDefaults.ApplicationKey]);
282299
Assert.Equal("EnvB", builder.Configuration[HostDefaults.EnvironmentKey]);
300+
Assert.Equal(tempPath, builder.Configuration[HostDefaults.ContentRootKey]);
283301

284302
Assert.Equal("AppB", builder.Environment.ApplicationName);
285303
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);
286307

287308
using IHost host = builder.Build();
288309

289310
var hostEnvironmentFromServices = host.Services.GetRequiredService<IHostEnvironment>();
290311
Assert.Equal("AppB", hostEnvironmentFromServices.ApplicationName);
291312
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);
292316
}
293317

294318
[Fact]

0 commit comments

Comments
 (0)