Skip to content

HostResolverFactory.ResolveServiceProviderFactory generates environment with incorrect IHostEnvironment.ApplicationName #96596

@daveidmx

Description

@daveidmx

Description

In the ResolveHostFactory(...) case, HostResolverFactory.ResolveServiceProviderFactory passes an --applicationName argument to the application it is "launching". The value for this argument is assembly.FullName, which differs from the value in HostBuilder, which is assembly.GetName().Name.

In practice, this means that an application whose natural IHostEnvironment.ApplicationName is "MyCompany.MyApp" will instead receive an application name of "MyCompany.MyApp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null".

This affects efcore tools. Any commands that resolve a DbContext instance will do so using an application host with the incorrect ApplicationName, affecting any application service wireup depending on that value.

Reproduction Steps

Copied from dotnet/efcore #32202 (not mine):

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.

Expected behavior

IHostEnvironment.ApplicationName is consistent between normal app launch and HostFactoryResolver launch.

Actual behavior

IHostEnvironment.ApplicationName is inconsistent between normal app launch and HostFactoryResolver launch.

Regression?

No response

Known Workarounds

No response

Configuration

PS> dotnet --list-sdks
8.0.100 [C:\Program Files\dotnet\sdk]

Other information

I believe this is the cause of dotnet/efcore #32202

Relevant files

Microsoft.Extensions.HostFactoryResolver/src/HostFactoryResolver.cs

args = args.Any(arg => IsApplicationNameArg(arg)) || assembly.FullName is null
    ? args
    : args.Concat(new[] { "--applicationName", assembly.FullName }).ToArray();

Microsoft.Extensions.Hosting/src/HostBuilder.cs

if (string.IsNullOrEmpty(applicationName))
{
    // Note GetEntryAssembly returns null for the net4x console test runner.
    applicationName = Assembly.GetEntryAssembly()?.GetName().Name;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions