Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting ApplicationName to random value when using CreateDefaultBuilder fails in Development #44467

Closed
AndreyTretyak opened this issue Nov 10, 2020 · 6 comments
Labels
area-Extensions-Hosting untriaged New issue has not been triaged by the area owner

Comments

@AndreyTretyak
Copy link

AndreyTretyak commented Nov 10, 2020

If you are using Host.CreateDefaultBuilder(args) to create HostBuilder, set environment to Development and set application name to a value other then assembly names used in this project following code path will fail with FileNotFoundException:

if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
{
    var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));

To me, it looks like it should not fail the whole host while trying to load dev secrets based on application name or if it's intended behavior then limitations in application name values should be specified clearly.

Code to reproduce

static async Task Main(string[] args)
{
	Environment.SetEnvironmentVariable("DOTNET_APPLICATIONNAME", "DummyName2");

	await Host.CreateDefaultBuilder(args)
		.UseEnvironment(Environments.Development)
		.Build()
		.RunAsync();
}

Or

static async Task Main(string[] args)
{
	await Host.CreateDefaultBuilder(args)
		.UseEnvironment(Environments.Development)
		.UseApplicationName("DummyName1")
		.Build()
		.RunAsync();
}

internal static IHostBuilder UseApplicationName(this IHostBuilder builder, string applicationName) =>
	builder.ConfigureHostConfiguration(configuration =>
	{
		configuration.AddInMemoryCollection(new[]
		{
			new KeyValuePair<string, string>(
				HostDefaults.ApplicationKey,
				applicationName)
		});
	});

Exception Information

Message:

System.IO.FileNotFoundException: 'Could not load file or assembly 'DummyName1, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

StackTrace:

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext) in /_/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs:line 345
at System.Reflection.Assembly.Load(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext) in /_/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs:line 73
at System.Reflection.Assembly.Load(AssemblyName assemblyRef) in /_/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs:line 55
at Microsoft.Extensions.Hosting.Host.<>c__DisplayClass1_0.<CreateDefaultBuilder>b__1(HostBuilderContext hostingContext, IConfigurationBuilder config) in /_/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs:line 81
at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration() in /_/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs:line 196
at Microsoft.Extensions.Hosting.HostBuilder.Build() in /_/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs:line 131
at ConsoleApp1.Program.<Main>d__0.MoveNext() in D:\Projects\WebApplication4\ConsoleApp1\Program.cs:line 12
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in /_/src/System.Private.CoreLib/shared/System/Runtime/ExceptionServices/ExceptionDispatchInfo.cs:line 63
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 180
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 151
at System.Runtime.CompilerServices.TaskAwaiter.GetResult() in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 107
at ConsoleApp1.Program.<Main>(String[] args)
@sakno
Copy link
Contributor

sakno commented Nov 10, 2020

You should not use ApplicationName property to define the app name. Here is the comment from David Fowler. The value of this property is reserved by DI internals. Check this line of code for more details.

Related issues and PRs:

@AndreyTretyak
Copy link
Author

Thank you for the details @sakno, if it required to be an executing assembly name (I'm not sure is it the case for Asp Net Core or for all application using GenericHost but looks like all of them need it) then maybe it should in fail in all cases not only with some special preconditions like using dev environment, also using other assembly names (ex, Newtonsoft.Json) would work fine :)

Maybe it should fail during HostBuilder.Build with a more meaningful error? What do you think?

Also @sakno in your comment "Check this line of code for more details."
you are pointing to the same line I was referencing in the issue is it the correct link?

@sakno
Copy link
Contributor

sakno commented Nov 10, 2020

Oops, sorry for my inattention. Yes, it's the same line of code. I also tried to use this property for app name but without luck. There are many places especially in ASP.NET Core MVC utilizing its special meaning.

@maryamariyan maryamariyan added area-Extensions-Hosting untriaged New issue has not been triaged by the area owner labels Nov 11, 2020
@ghost
Copy link

ghost commented Nov 11, 2020

Tagging subscribers to this area: @eerhardt, @maryamariyan
See info in area-owners.md if you want to be subscribed.


Issue meta data
Issue content: If you are using `Host.CreateDefaultBuilder(args)` to create `HostBuilder`, set environment to `Development` and set application name to a value other then assembly names used in this project [following code path](https://github.com/dotnet/runtime/blob/6a5a78bec9a6e14b4aa52cd5ac558f6cf5c6a211/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs#L81) will fail with `FileNotFoundException`: ```CSharp if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName)) { var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName)); ```

To me, it looks like it should not fail the whole host while trying to load dev secrets based on application name or if it's intended behavior then limitations in application name values should be specified clearly.

Code to reproduce

static async Task Main(string[] args)
{
	Environment.SetEnvironmentVariable("DOTNET_APPLICATIONNAME", "DummyName2");

	await Host.CreateDefaultBuilder(args)
		.UseEnvironment(Environments.Development)
		.Build()
		.RunAsync();
}

Or

static async Task Main(string[] args)
{
	await Host.CreateDefaultBuilder(args)
		.UseEnvironment(Environments.Development)
		.UseApplicationName("DummyName1")
		.Build()
		.RunAsync();
}

internal static IHostBuilder UseApplicationName(this IHostBuilder builder, string applicationName) =>
	builder.ConfigureHostConfiguration(configuration =>
	{
		configuration.AddInMemoryCollection(new[]
		{
			new KeyValuePair<string, string>(
				HostDefaults.ApplicationKey,
				applicationName)
		});
	});

Exception Information

Message:

System.IO.FileNotFoundException: 'Could not load file or assembly 'DummyName1, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'

StackTrace:

at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, RuntimeAssembly assemblyContext, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, AssemblyLoadContext assemblyLoadContext)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext) in /_/src/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs:line 345
at System.Reflection.Assembly.Load(AssemblyName assemblyRef, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext) in /_/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs:line 73
at System.Reflection.Assembly.Load(AssemblyName assemblyRef) in /_/src/System.Private.CoreLib/src/System/Reflection/Assembly.CoreCLR.cs:line 55
at Microsoft.Extensions.Hosting.Host.<>c__DisplayClass1_0.<CreateDefaultBuilder>b__1(HostBuilderContext hostingContext, IConfigurationBuilder config) in /_/src/libraries/Microsoft.Extensions.Hosting/src/Host.cs:line 81
at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration() in /_/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs:line 196
at Microsoft.Extensions.Hosting.HostBuilder.Build() in /_/src/libraries/Microsoft.Extensions.Hosting/src/HostBuilder.cs:line 131
at ConsoleApp1.Program.<Main>d__0.MoveNext() in D:\Projects\WebApplication4\ConsoleApp1\Program.cs:line 12
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() in /_/src/System.Private.CoreLib/shared/System/Runtime/ExceptionServices/ExceptionDispatchInfo.cs:line 63
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 180
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 151
at System.Runtime.CompilerServices.TaskAwaiter.GetResult() in /_/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:line 107
at ConsoleApp1.Program.<Main>(String[] args)
```</td>
  </tr>
  <tr>
    <td>Issue author:</td>
    <td>AndreyTretyak</td>
  </tr>
  <tr>
    <td>Assignees:</td>
    <td>-</td>
  </tr>
  <tr>
    <td>Milestone:</td>
    <td>-</td>
  </tr>
  </table>
  </details>

@maryamariyan
Copy link
Member

You should not use ApplicationName property to define the app name. Here is the comment from David Fowler. The value of this property is reserved by DI internals. Check this line of code for more details.

Closing issue as by design

@AndreyTretyak
Copy link
Author

@maryamariyan should not docs and error message be more explicit about this problem?

@ghost ghost locked as resolved and limited conversation to collaborators Apr 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Hosting untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants