-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Remove GetCallingAssembly usage #35063
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
Conversation
- Today the WebApplicationBuilder tries to guess what the application name should be based on the immediate caller of the API using Assembly.GetCallingAssembly. This is because several components use the application name as a way to find the application assembly in order to do other things; - User secrets uses it to find the location of the directory that holds configuration secrets. - MVC uses it as the assembly to start the search for controllers, pages, etc. - Identifty uses it to find application parts. This API is a bit fragile for internal refactoring (if the immediate caller is anything except for user code, then we will treat our own assembly as the calling assembly) and more importantly, it breaks in NativeAOT scenarios. Instead of the calling assembly, we now default to the entry assembly. - This will "break" in scenarios where the WebApplication is started in a library and the entry point is the host application (controllers will be scanned in potentially the wrong place by default or it will find the wrong controllers). - Now that we have the option that allows specifying the name of the application name via WebApplicationOptions, In the future, we're investigating ways that the compiler can inject the calling assembly via an attribute similar to [CallerMemberName] that would capture the calling assembly in a more reliable and AOT friendly way. - Added a test that asserts that the default application name is the entry assembly. - Updated the slnf file
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
| // We need to override the application name since the call to Configure will set it to | ||
| // be the calling assembly's name. | ||
| webHostBuilder.UseSetting(WebHostDefaults.ApplicationKey, (callingAssembly ?? Assembly.GetEntryAssembly())?.GetName()?.Name ?? string.Empty); | ||
| webHostBuilder.UseSetting(WebHostDefaults.ApplicationKey, (Assembly.GetEntryAssembly())?.GetName()?.Name ?? string.Empty); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want people to be able ot set the assembly name directly from the options for the controllers in libraries scenarios?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is just the default. The API I added (WebApplicationOptions) allows setting the name. Or you can set it via command line args
aspnetcore/src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs
Line 387 in 8b06da3
| public void WebApplicationBuilderApplicationNameCanBeOverridden() |
PS: You just made me realize I should update this test to be a different assembly name, now that the entry assembly is the default!
- Change the name of the overriding assembly since the entry assembly is the default.
| public void WebApplicationBuilderApplicationNameCanBeOverridden() | ||
| { | ||
| var assemblyName = Assembly.GetEntryAssembly().GetName().Name; | ||
| var assemblyName = typeof(WebApplicationTests).Assembly.GetName().Name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just pick a random name so we don't accidently lose coverage in case we revert behavior or something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only reason it could be random is because we're not loading the assembly in this situation. It should be a real assembly name...
This API is a bit fragile for internal refactoring (if the immediate caller is anything except for user code, then we will treat our own assembly as the calling assembly) and more importantly, it breaks in NativeAOT scenarios. Instead of the calling assembly, we now default to the entry assembly.
Fixes #34838