dotnet / dotnet/aspnetcore

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

Open
#52,152 4 comments 1 reaction 1 assignee Claimed by @gfoidl View on GitHub
area-hosting bug
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

Repro:
```c#
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 https://github.com/dotnet/aspnetcore/blob/4a156ba645cc1910033ec114a770c8ce91505470/src/DefaultBuilder/src/WebApplicationBuilder.cs#L280-L281 where the assembly is loaded via the `env.ApplicationName` which in turn is set by https://github.com/dotnet/aspnetcore/blob/4a156ba645cc1910033ec114a770c8ce91505470/src/Hosting/Hosting/src/Internal/WebHostOptions.cs#L20

In order to work for every ApplicationName should the `WebApplicationBuilder` be changed to
```diff
-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
```c#
if (builder.Environment.IsDevelopment())
{
builder.Configuration.AddUserSecrets(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 https://github.com/dotnet/aspnetcore/blob/4a156ba645cc1910033ec114a770c8ce91505470/src/Hosting/Hosting/src/Internal/WebHostOptions.cs#L33
That should be updated too.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.