-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
Make custom ApplicationName work for hosting and user secrets #52305
Conversation
…t, otherwise fallback to Assembly.GetEntryAssembly
Thanks for your PR, @gfoidl. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
} | ||
catch | ||
{ | ||
return Assembly.GetEntryAssembly(); |
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.
In unit tests this can be testhost
, but that's no problem as the user secrets are reigstered as optional
, thus if the attribute for user sectes doesn't exist, it's just ignored.
Besides that is there any other reliable way to probe for existance of an assembly?
Hey @gfoidl are you also planning to backport this to net8.0? |
We should avoid first chance exceptions in the default code path. I haven't been following this issue closely but why do we need this change? |
I agree, unfortunately we already use a setting called While trying to update to net8.0 this part broke with assembly not found error, to overcome the issue I had to use a different configuration name (e.g. I can do that of course but I have quite few services to update and it's working fine in net6.0 so I was wondering if we can keep it this way also with net8.0 |
@davidfowl just try a default app (from the template) and change the app name, like WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ApplicationName = "MySuperApp has a name that doesn't match the name of the entry assembly"
}); then host startup will crash. As the app name can be set, and it's documented that it can be changed, it's very strange that it won't work with that anymore. Unfortunately I don't have any better solution than probing at my hands. Other solution would have more impact, like additional configuration to explicitly set the startup assembly when the app name is change, which would deserve a nice exception message, etc. (and I don't have very much time ATM to evaluate more ways, but I'm very open for suggestions) BTW: that probing with the |
The name has to be an assembly name. Are you saying it stops the app from starting in .NET 8 but didn't in .NET 6? |
Yes, we introduced this break when we added the Also, I don't think we say anything in the documentation or through runtime exceptions that tells people their |
Ah, new information for me 😉 So I see two solutions:
I'm fine either way, as there are workarounds available, though I lean a little bit towards the first bullet, as otherwise ApplicationName should actually be HostingStartupAssemblyName or that like. As background why I'm using a different ApplicationName: |
The fix in this PR isn't complete, there's I'm waiting for a decision on whether it's a documentation issue or the one I'm trying to fix here. |
I'm in favor of this approach. We don't do anything to document or enforce the requirement that |
|
||
try | ||
{ | ||
_ = Assembly.Load(new AssemblyName(environment.ApplicationName)); |
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.
I'm not really happy with that solution.
What if we extend IWebHostEnvironment
with a property for the HostingStartupAssemblyName
?
Then these checks just need to be done once, the property-value can be used whereever needed.
Also the logic for determing the correct assembly name is only in one place.
Yes, its assumed and it has to be an assembly name, it breaks more than just the host it also breaks other parts of the framework. MVC also blows up. I'm not sure we should "fix" this. At least not in this way. Fixing this properly means introducing another property than isn't used in any of these code paths. I'm in favor of that because it would actually work everywhere. |
@davidfowl so something like outlined in #52305 (comment) |
Then introduce a new propert on public interface Microsoft.Extensions.Hosting.IHostEnvironment
{
string EnvironmentName { get; set; }
string ApplicationName { get; set; }
+ string HostingStartupAssemblyName { get; set; }
string ContentRootPath { get; set; }
IFileProvider ContentRootFileProvider { get; set; }
} which clearly indicates what name it should be. A different approach is to keep So far I've written about |
I know we keep saying that but we’ve been using it this way since the beginning and to fix it properly we need to add a new property. I agree this change should be done in the runtime. |
Fine. I'll create an API proposal over there (but not before next week). |
Make custom ApplicationName work for hosting and user secrets
Summary of the changes (Less than 80 chars)
Allow configuring a custom ApplicationName w/o crash of the host
Description
For startup assemblies and user secrets the assembly is loaded by
IHostEnvironment.ApplicationName
.If that app name isn't the default one, which is constructed from the entry assembly, then those assemblies may not exist and startup crashes.
This change probes if that assembly actually can be loaded, if so use it, otherwise fallback to the entry assembly.
Fixes #52152
Note: in my local setup I'm unable to build the functional tests, they hang. But I'm quite sure this has to do with my local setup, thus I'll let CI decide if it's good or not.