diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs index 87e7149ceadca9..b4d493695f125e 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/ref/Microsoft.Extensions.Hosting.Abstractions.cs @@ -122,7 +122,7 @@ public partial interface IHostedService } public partial interface IHostEnvironment { - string? ApplicationName { get; set; } + string ApplicationName { get; set; } Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider { get; set; } string ContentRootPath { get; set; } string EnvironmentName { get; set; } @@ -130,7 +130,7 @@ public partial interface IHostEnvironment [System.ObsoleteAttribute("IHostingEnvironment has been deprecated. Use Microsoft.Extensions.Hosting.IHostEnvironment instead.")] public partial interface IHostingEnvironment { - string? ApplicationName { get; set; } + string ApplicationName { get; set; } Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider { get; set; } string ContentRootPath { get; set; } string EnvironmentName { get; set; } diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs index 90c1ae296198d9..6342dc66165976 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostEnvironment.cs @@ -21,7 +21,7 @@ public interface IHostEnvironment /// Gets or sets the name of the application. This property is automatically set by the host to the assembly containing /// the application entry point. /// - string? ApplicationName { get; set; } + string ApplicationName { get; set; } /// /// Gets or sets the absolute path to the directory that contains the application content files. diff --git a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs index abba619c76d2eb..af94ab7c1b59c3 100644 --- a/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs +++ b/src/libraries/Microsoft.Extensions.Hosting.Abstractions/src/IHostingEnvironment.cs @@ -26,7 +26,7 @@ public interface IHostingEnvironment /// Gets or sets the name of the application. This property is automatically set by the host to the assembly containing /// the application entry point. /// - string? ApplicationName { get; set; } + string ApplicationName { get; set; } /// /// Gets or sets the absolute path to the directory that contains the application content files. diff --git a/src/libraries/Microsoft.Extensions.Hosting/ref/Microsoft.Extensions.Hosting.cs b/src/libraries/Microsoft.Extensions.Hosting/ref/Microsoft.Extensions.Hosting.cs index 22e8f9b805aa72..cc1d7d84b9bd1c 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/ref/Microsoft.Extensions.Hosting.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/ref/Microsoft.Extensions.Hosting.cs @@ -133,7 +133,7 @@ public void Dispose() { } public partial class HostingEnvironment : Microsoft.Extensions.Hosting.IHostEnvironment, Microsoft.Extensions.Hosting.IHostingEnvironment { public HostingEnvironment() { } - public string? ApplicationName { get { throw null; } set { } } + public string ApplicationName { get { throw null; } set { } } public Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider { get { throw null; } set { } } public string ContentRootPath { get { throw null; } set { } } public string EnvironmentName { get { throw null; } set { } } diff --git a/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs b/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs index 6c283b030041f3..59023d32ac2415 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs @@ -216,15 +216,20 @@ internal static (HostingEnvironment, PhysicalFileProvider) CreateHostingEnvironm { var hostingEnvironment = new HostingEnvironment() { - ApplicationName = hostConfiguration[HostDefaults.ApplicationKey], EnvironmentName = hostConfiguration[HostDefaults.EnvironmentKey] ?? Environments.Production, ContentRootPath = ResolveContentRootPath(hostConfiguration[HostDefaults.ContentRootKey], AppContext.BaseDirectory), }; - if (string.IsNullOrEmpty(hostingEnvironment.ApplicationName)) + string? applicationName = hostConfiguration[HostDefaults.ApplicationKey]; + if (string.IsNullOrEmpty(applicationName)) { // Note GetEntryAssembly returns null for the net4x console test runner. - hostingEnvironment.ApplicationName = Assembly.GetEntryAssembly()?.GetName().Name; + applicationName = Assembly.GetEntryAssembly()?.GetName().Name; + } + + if (applicationName is not null) + { + hostingEnvironment.ApplicationName = applicationName; } var physicalFileProvider = new PhysicalFileProvider(hostingEnvironment.ContentRootPath); diff --git a/src/libraries/Microsoft.Extensions.Hosting/src/Internal/HostingEnvironment.cs b/src/libraries/Microsoft.Extensions.Hosting/src/Internal/HostingEnvironment.cs index c813f4d9cd9cb9..34cd3a4ee5f399 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/src/Internal/HostingEnvironment.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/src/Internal/HostingEnvironment.cs @@ -13,11 +13,11 @@ namespace Microsoft.Extensions.Hosting.Internal public class HostingEnvironment : IHostingEnvironment, IHostEnvironment #pragma warning restore CS0618 // Type or member is obsolete { - public string EnvironmentName { get; set; } = null!; + public string EnvironmentName { get; set; } = string.Empty; - public string? ApplicationName { get; set; } + public string ApplicationName { get; set; } = string.Empty; - public string ContentRootPath { get; set; } = null!; + public string ContentRootPath { get; set; } = string.Empty; public IFileProvider ContentRootFileProvider { get; set; } = null!; } diff --git a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostApplicationBuilderTests.cs b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostApplicationBuilderTests.cs index 4b449be98c4100..440e006ea6f2e3 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostApplicationBuilderTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostApplicationBuilderTests.cs @@ -206,7 +206,7 @@ public void DisableDefaultIHostEnvironmentValues() Assert.NotNull(builder.Environment.ApplicationName); #elif NETFRAMEWORK // Note GetEntryAssembly returns null for the net4x console test runner. - Assert.Null(builder.Environment.ApplicationName); + Assert.Equal(string.Empty, builder.Environment.ApplicationName); #else #error TFMs need to be updated #endif @@ -221,7 +221,7 @@ public void DisableDefaultIHostEnvironmentValues() Assert.NotNull(env.ApplicationName); #elif NETFRAMEWORK // Note GetEntryAssembly returns null for the net4x console test runner. - Assert.Null(env.ApplicationName); + Assert.Equal(string.Empty, env.ApplicationName); #else #error TFMs need to be updated #endif diff --git a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs index 2f7aaca8cbaecd..524543124033c1 100644 --- a/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs +++ b/src/libraries/Microsoft.Extensions.Hosting/tests/UnitTests/HostBuilderTests.cs @@ -178,7 +178,7 @@ public void DefaultIHostEnvironmentValues() Assert.NotNull(env.ApplicationName); #elif NETFRAMEWORK // Note GetEntryAssembly returns null for the net4x console test runner. - Assert.Null(env.ApplicationName); + Assert.Equal(string.Empty, env.ApplicationName); #else #error TFMs need to be updated #endif @@ -194,7 +194,7 @@ public void DefaultIHostEnvironmentValues() Assert.NotNull(env.ApplicationName); #elif NETFRAMEWORK // Note GetEntryAssembly returns null for the net4x console test runner. - Assert.Null(env.ApplicationName); + Assert.Equal(string.Empty, env.ApplicationName); #else #error TFMs need to be updated #endif