dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.23k stars 9.95k forks source link

Single file exe + razor nuget breaks routing in asp.net core application #26151

Open jjxtra opened 4 years ago

jjxtra commented 4 years ago

Describe the bug

In any asp.net core project using the new .NET 5 single file exe publish command, routing is broken due to the use of Assembly.CodeBase. This impacts routing calls such as:

There are probably others.

To Reproduce

See attached zip at bottom of thread. This appears to be a problem of adding some razor functionality to the service collection.

Run the published exe with a --urls parameter and then open your browser and attempt to hit the weather forecast end point.

Exceptions (if any)

Use of Assembly.CodeBase throws not supported exception and application crashes.

Further technical details

For servers obviously this can be worked around, just turn off the single file option. For client software that uses a web browser UI however this is unfortunate as the single file exe vastly reduces the number of files the client has to mess with to get the application deployed.

pranavkm commented 4 years ago

What specific version of ASP.NET Core are you using? We fixed issues with using Views in single file exes as part of RC1, but the web api template has been functional with single file publishing for a few previews now.

jjxtra commented 4 years ago

dotnet --version : 5.0.100-rc.1.20452.10 - downloaded SDK from Sept. 15 2020.

jjxtra commented 4 years ago

If it helps, I am using controllers that are in a referenced shared library project and I am setting the IHostEnvironment AppName to the entry assembly name, that might be where the breakage happens.

jjxtra commented 4 years ago

I am in process of trying to get a minimal repro together...

jjxtra commented 4 years ago

Ok I think I've got it, here is my sample project:

WebApplication1.zip

The biggest call out is that Startup.cs line 39 is registering some razor functionality. It looks like commenting this out removes the CodeBase error. Is there an alternative way I should be registering Razor for .NET 5?

jjxtra commented 4 years ago

Looks legit, even updating the razor compilation nuget to .net 5 fails to solve the error. Is there a chance this will be fixed in .NET 5 RC 2 or RTM?

pranavkm commented 4 years ago

You could look at enabling runtime compilation conditionally the way our templates do: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation?view=aspnetcore-3.1&tabs=visual-studio#conditionally-enable-runtime-compilation-in-an-existing-project.

While this is a bug, we do not expect to support runtime compilation which requires lots of files on disk to work with single file exe, which makes it unlikely to be fixed in this release.

ghost commented 4 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

jjxtra commented 4 years ago

Thank you.

yaobo-lab commented 2 years ago

@jjxtra Is there a solution to this problem? I have the same problem. How to solve?

jjxtra commented 2 years ago

I had to add a hack to set the application name to the entry assembly.

yaobo-lab commented 2 years ago

I had to add a hack to set the application name to the entry assembly.

how to do? Can you guide me