Skip to content

WebApplication.CreateBuilder with non-default ApplicationName doesn't load user secrets #52152

Open
@gfoidl

Description

@gfoidl

Repro:

WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
    Args            = args,
    ApplicationName = "MySuperApp has a name that doesn't match the name of the entry assembly"
});

Now if you use any config value that should be set / overwritten by user secrets won't have these used.

This stems from

var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
configuration.AddUserSecrets(appAssembly, optional: true, reloadOnChange: reloadOnChange);
where the assembly is loaded via the env.ApplicationName which in turn is set by
ApplicationName = environment?.ApplicationName ?? GetConfig(WebHostDefaults.ApplicationKey) ?? Assembly.GetEntryAssembly()?.GetName().Name ?? string.Empty;

In order to work for every ApplicationName should the WebApplicationBuilder be changed to

-var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
+var appAssembly = Assembly.GetEntryAssembly();
configuration.AddUserSecrets(appAssembly, optional: true, reloadOnChange: reloadOnChange);

?

Workaround: just add the user secrets manually by

    if (builder.Environment.IsDevelopment())
    {
        builder.Configuration.AddUserSecrets<Program>(optional: true, reloadOnChange: true);
    }

But it feels strange if one can set the ApplicationName, and then some things won't work as expected.
Alternatively the docs should reflect this. I prefer changing the code though.


Edit: startup fails also due to

HostingStartupAssemblies = Split(ApplicationName, GetConfig(WebHostDefaults.HostingStartupAssembliesKey));

That should be updated too.

Metadata

Metadata

Assignees

Labels

area-hostingIncludes HostingbugThis issue describes a behavior which is not expected - a bug.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions