dotnet / dotnet/AspNetCore.Docs

It's not documented that ApplicationName in ASP.NET Core project can not be an arbitrary string

Open
#30,574 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

# Issue description

Starting from the scratch ASP.NET Core 7 project I wanted have application name string available in various parts of the project code. For that I went to use `IHostEnvironment.ApplicationName` because it sounded logical to me.

I tried to set this value in `appsettings.json` configuration like this:
```
{
"ApplicationName": "AppNameFromConfig",
}
```
But that wasn't picked up.

Then I tried to set it in code like this:
```csharp
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSetting(WebHostDefaults.ApplicationKey, "MyAppName");
```
But I got exception:
```
System.NotSupportedException: 'The application name changed from "MySampleWebApp1" to "MyAppName".
Changing the host configuration using WebApplicationBuilder.WebHost is not supported.
Use WebApplication.CreateBuilder(WebApplicationOptions) instead.'

```

So I followed the instructions from the exception and tried this:
```csharp
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
ApplicationName = "MyAppName",
Args = args
});
```

but then I got another exception:
```
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in System.Private.CoreLib.dll
Could not load file or assembly 'MyAppName, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
```

Exploring the source code of `WebApplication.CreateBuilder` method I found out that the this property must be assembly name and can't be arbitrary string like I was trying to set.

I think this should be noted in the docs to save developers from hassle of going through all this and figuring it out on their own.

I also found this related discussion https://github.com/aspnet/Hosting/issues/1179 and comment that explains the bad naming used for this property https://github.com/aspnet/Hosting/issues/1179#issuecomment-323597195

# Software versions

* [x] ASP.NET Core 6 or later

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.