Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ 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; }
}
[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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
string? ApplicationName { get; set; }
string ApplicationName { get; set; }

/// <summary>
/// Gets or sets the absolute path to the directory that contains the application content files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
string? ApplicationName { get; set; }
string ApplicationName { get; set; }

/// <summary>
/// Gets or sets the absolute path to the directory that contains the application content files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { } }
Expand Down
11 changes: 8 additions & 3 deletions src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down