Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

MVC assumes IHostingEnvironment.ApplicationName contains the entry point assembly's name #6685

Closed
MisterGoodcat opened this issue Aug 19, 2017 · 8 comments

Comments

@MisterGoodcat
Copy link

Some parts of MVC seem to assume that IHostingEnvironment.ApplicationName actually contains an assembly name.

The situation:

  • We implemented a custom application container (think generic entry point assembly to bootstrap a pre-configured environment + pipeline etc. for lightweight services/applications).
  • We set the ApplicationName property so it matches whatever the actual implementation is handed off to (an application-level project/assembly).
  • Our arbitrary application names often do not match assembly names.

Typically these applications have no views (only RESTful APIs), so we didn't come across any issues until now. But this one has a minimal UI too, and the result was a FileNotFoundException buried deep in a stack trace down to the RazorViewCompiler.

It took some digging in the sources to find that the CSharp compiler tries to load an assembly based on that property, and looking further revealed that there are other places in the code base, for example around the assembly parts discovery, that rely on IHostingEnvironment.ApplicationName returning an assembly name.

The good news is that today I learned about the concept of dependency context and its compilation options. The bad side of this was that it was super-non-obvious what happens and that it took too long to understand what the actual problem is.

"ApplicationName" is really bad semantics for this, but I guess that's baked now and cannot be changed. At least make the documentation very clear on what the purpose/intention in the context of MVC is, so people don't change it randomly to "nicer" things. The documentation of IHostingEnvironment simply says it's set to entry point assembly name, but the documentation on hosting fundaments says you can happily change this and even shows (bad) sample code of how to set it to something like "MyApp". Btw. there seems to exist an older issue for that (milestone 1.0.1) that apparently never had been completed.

@davidfowl
Copy link
Member

The doc does needs an update. We should remove that configuring the application name to something arbitrary.

/cc @Rick-Anderson @tdykstra

@Rick-Anderson
Copy link
Contributor

@MisterGoodcat would you be able to create a PR or draft the changes needed?
/cc @guardrex

@guardrex
Copy link

I'll note on dotnet/AspNetCore.Docs#1870 that it might surface setting <AssemblyName> in the project file, but the topic that we would hope to link doesn't actually exist yet: Documenting .NET Core MSBuild targets and properties/CSPROJ (docs/#2642). It would prob link to Common MSBuild Project Properties.

@MisterGoodcat If u just want to PR a one-liner to cover this, you don't need to sweat the full dotnet/AspNetCore.Docs#1870 ... I added it to a list of backlog issues that I hope to address in the coming few weeks. Just note in the issue comment: Addresses #1870; or conversely if u prefer I shoot a quick one/two line fix in, let me know, and I'll do that.

@guardrex
Copy link

guardrex commented Aug 19, 2017

@davidfowl Are u saying "remove" as in "remove it completely" or as in "just remove the arbitrary value in setting it"?

How about something like ...

new WebHostBuilder()
    .UseSetting("applicationName", "MyDependencyContextAssemblyName")

... along with a combined note based on current language, IHostingEnvironment interface text, [@]danroth27's language, and a note on the <AssemblyName> ...

This configuration setting specifies the value for `IHostingEnvironment.ApplicationName`. This property is automatically set by the host to the assembly containing the application entry point. You can set the property in cases where you want to specify the dependency context assembly yourself. If you wish to set the app's assembly name, use the [\<AssemblyName>](https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties) property in your project file.

@davidfowl
Copy link
Member

@davidfowl Are u saying "remove" as in "remove it completely" or as in "just remove the arbitrary value in setting it"?

Remove it completely.

@guardrex
Copy link

I'll patch it now.

@MisterGoodcat
Copy link
Author

Hi and sorry for the late reply, I was out of town for a few days. Do you still need anything from me?

@guardrex
Copy link

@MisterGoodcat The AssemblyName host config language was removed from the fundamentals/hosting topic. They may introduce a FriendlyName in v3 👉 aspnet/Hosting#1180 (comment). AFAICT u can close this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants