Skip to content

The value of HostingEnvironment.ApplicationName is incorrect during EF migrations #32202

Description

@crozone

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When using the newer .NET 6+ minimal startup code:

WebApplicationBuilder builder = WebApplication.CreateBuilder(args)

The value of builder.Environment.ApplicationName is usually set to the equivalent of Assembly.GetEntryAssembly().GetName().Name.

For example, "MyApplication.Web". This is expected.

However, during design time (when using tools like dotnet ef), the value of builder.Environment.ApplicationName instead becomes the full Assembly.GetEntryAssembly().GetName().ToString().

For example: "MyApplication.Web, Version=2.5.4.0, Culture=neutral, PublicKeyToken=null"

Which is unexpected and causes issues in startup code that expects the application name to be consistent regardless of context.

Expected Behavior

The value of WebApplicationBuilder.Environment.ApplicationName should be consistent regardless of whether the application is run normally, or during design time via dotnet ef tooling.

The expected value is the equivalent of Assembly.GetEntryAssembly().GetName().Name.

Steps To Reproduce

Create a standard ASP.NET Core Web app project, with the .NET 7.0 Framework, named "AspNetCoreTemplateTest".

Replace the startup code with the following Main() method:

public static void Main(string[] args)
{
    var builder = WebApplication.CreateBuilder(args);
    Console.WriteLine(builder.Environment.ApplicationName);
}

Run the application normally. Observe that "AspNetCoreTemplateTest" is printed as the builder.Environment.ApplicationName.

Run dotnet ef migrations list. Observe that "AspNetCoreTemplateTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" is printed as the builder.Environment.ApplicationName.

Exceptions (if any)

We configure a UserAgent as part of our ElasticSearch Serlilog configuration during startup. Part of this requires passing hostingContext.HostingEnvironment.ApplicationName into a ProductInfoHeaderValue:

new ProductInfoHeaderValue($"{hostingContext.HostingEnvironment.ApplicationName}-{hostingContext.HostingEnvironment.EnvironmentName}", Assembly.GetEntryAssembly()?.GetName().Version?.ToString()).ToString());

This fails when the ApplicationName contains characters that are invalid for a HTTP header.

.NET Version

7.0.307

Anything else?

  • Microsoft.AspNetCore.App version 7.0.10
  • dotnet-ef 7.0.10
  • Visual Studio 2022 64-bit Version 17.6.2

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions