Skip to content

Conversation

@davidfowl
Copy link
Member

  • 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

Fixes #34838

- 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
@davidfowl
Copy link
Member Author

/azp run

@azure-pipelines
Copy link

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);
Copy link
Member

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?

Copy link
Member Author

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

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;
Copy link
Member

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?

Copy link
Member Author

@davidfowl davidfowl Aug 5, 2021

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...

@davidfowl davidfowl merged commit e3b6407 into main Aug 5, 2021
@davidfowl davidfowl deleted the davidfowl/remove-calling-assembly branch August 5, 2021 19:55
@ghost ghost added this to the 6.0-rc1 milestone Aug 5, 2021
@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-hosting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minimal host does not work with NativeAOT

5 participants