Self-Contained Single-File Win Form app does not run - .net-core

I am on VS 2019 16.8.3 with .Net 5.0.
I have created a simple Win Form project without any NuGet or project dependency. I have made sure it runs out of the IDE.
When I published the project to a self-contained single file exe, it does not run. The Task explorer briefly showed the exe and switch it to suspended before killing it. The exe works fine after I republished it either without the single-file or the self-contained option. It seems like both options cannot co-exist.
Any idea?

Related

Why all .NET Core applications are console applications?

I have a very basic question about why all .NET Core applications are console applications. I did some google search but don't find any appropriate answer.
It's an interesting question, and I got here because I had the same thought: Why do I seem to always see a console on asp.net core apps? Are they all now console apps?
But it's a mirage. While you are seeing a console window, it's only because the code is using the console for output. The code that starts the app governs where the output is going to. For example, instead, the output could be ported to the Windows Event Log, which is what happens when the site is published to IIS.
If you create a default .netcore web app, and you look in the output directory, you will see two files:
YourProject.exe
YourProject.dll
If you double click the exe, it will start the website. You will see the console window appear and the app will start listening on port 5000.
But now rename YourProject.dll and try to run the exe. It will fail because the exe is just the startup code. Your website is actually in the .dll.
The point is that the console window you see when you start an web app is coming from the exe, not the dll. So the console window has nothing to do with your web app, it's just a convenient way to start up the app in a dev environment. You can also start it from the command line with dotnet YourProject.dll and it will run exactly the same as if you started it from YourProject.exe
If you read #HansPassant's comment, he says it exactly correctly, my answer is just an expansion of it.
.NET Core provides more than console applications out of the box, it has supported both WPF and WinForms since version 3.x. In the below documentation links from Microsoft you can see .NET 5.0 (which is .NET Core) is listed. If you're using Visual Studio you can chose between ".NET" (which is .NET Core and what it will be referred to going forward) and ".NET Framework" (which won't have any versions past 4.8). It should be noted, WPF and WinForms although on .NET Core are only supported on Windows.
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/?view=netdesktop-5.0
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/?view=netdesktop-5.0

.NET Core 31 - Single EXE - The application to execute does not exist

Converted an old .NET Framework project to .NET Core 3.1 yesterday. The application runs fine on my own machine and debugs correctly.
I have put both the following into my .vbproj/.csproj
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win81-x64</RuntimeIdentifier>
However when I move the .EXE to a remote machine and attempt to run said EXE on that remote machine I get told that the DLL (for the project in question) does not exists.
I moved the .dll to the same directory that the exe is in (not ideal) and get the following error instead
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
I used to use Costura.Fody to create a single file exe without any issues in .NET Framework, I was under the impression the above would do the same for the .NET Core packages.
Can someone point me in the right direction please.
For anyone else that may find this question and find it useless.
I was 'Building' the project instead of 'Publishing' the project, this in-turn wasn't creating the self-contained exe properly.
Don't be a donut like me.
In Visual Studio 2019, go to Build > Publish MyApp. This will open a tab in the editor area. Click on Show all settings to open the Profile settings dialog. Then click on File publish options and check Produce single file.

How do I run an ASP.Net Core executable file?

I am new to ASP.Net Core. I have been following a tutuorial. I got to the point at which Javascript code was added to my project. I ran my program, and nothing in the script worked. After adding a few alert() calls, it was apparent that my program was not finding my script. People here and at Reddit suggested I look at the network tab of DevTools. I saw that my program was not able to find any js or css files.
Being an old-school C++/C# developer, I was running the program from a command window by going to the folder containing the executable file and typing the name of the file.
I went back to the beginning. The tutorial directs me to use the dotnet command, which is brand new to me. The first time it tells me to run my program, it tells me to go to the project directory and enter "dotnet run". It just hit me like a ton of bricks that that was what I should have done the first time. I went back to that project, used "dotnet run", and it worked.
So, my question is this: is it possible to run MyProject.exe by itself? If so, how? Copy it into my project folder? Or is the "dotnet run" command the standard way of executing an ASP.Net Core application?
I think it won't work if you directly run it (not sure if we can run MVC project using dotnet-cli (dotnet run will try to load only MSIL to CLR)).
DotNet core console application is different from .net core MVC projects
you can run your project by following ways.
1. Run using visual studio
when you create an MVC application, in the visual studio there will be multiple options to run it, you can choose IISexpress one, it will open in the default browser.
for step by step guide -> https://learn.microsoft.com/en-us/visualstudio/ide/quickstart-aspnet-core?view=vs-2019
2. publish it in IIS
For this you need to ensure few things, like hosting bundle is installed or not, IIS is correctly configured for core application or not.
for step by step guide -> https://stackify.com/how-to-deploy-asp-net-core-to-iis/
3. Use VSCode
for step by step guide -> https://learn.microsoft.com/en-us/aspnet/core/tutorials/publish-to-azure-webapp-using-vscode?view=aspnetcore-3.1

Debugging and rebuilding a .net core web app

How do I actually run the debugger with a .net core web app using visual studio along side the dotnet watch tool?
I am using the dot net watch tool which greats work and picks up changes, however if I try to attach the debugger, the code doesn't match with what's compiled. So I have to ctrl-c and stop the hosted app, rebuild and rerun donet run and reattach the app to the dotnet process. Surely this can't be the workflow of .net core developers?

Visual Studio 2017 Doesn't Run Core Apps with IIS Express

I am having an issue with Visual Studio 2017 RC where when I run an MVC and/or WebAPI app using IIS Express the app never actually runs. Instead I get stuck with a page in the browser indicating it is trying to attach and from there it goes no further. If I open another tab/window in the browser and try to navigate directly to the launch URL, it just spins forever. Likewise when I run this using the dot net run command, the app launches and runs fine.
There do not seem to be any errors that I can see, just that the browser spins indefinitely without actually ever loading the page/endpoint.
Note: this occurs for most any circumstance. Newly created project in VS with no changes, known working project from previous versions, etc.
You could follow or upvote the issue: https://developercommunity.visualstudio.com/content/problem/11391/aspnet-core-iis-express-httplocalhost51733-failed.html
Looks like our workarounds meanwhile are
dotnet run
Publish to local IIS. Which is working for me.
(Untested: downgrade from core-1.1 to core-1.0)
If you aren't familiar with VS2017/asp.net:
You have, or can reveal via View Menu-->Other Toolbars :
Debug toolbar --> dropdowncombobutton saying "[green arrow>] IIS Express"
Click on the dropdown downarrow part of the button.
You should see a Run {yourprojectname} option.
This is the equivalent of dotnet run {yourprojectname} from the commandline.
You can configure the options that appear in the dropdown with
Right-click on project in solution explorer --> Properties --> Debug
I had this same issue and after digging around for a while I discovered that I had dotnet preview v1.0.0-preview2 installed under Programs Files (x86) and a non-preview version in x64. I think VS is launching the x86 (preview) version but expecting to see the full version. To fix this, I did the following.
from programs and feature uninstall every visible dotnet core. (note: for me this did not remove the x86 preview)
go to https://www.microsoft.com/net/download/core#/sdk and install BOTH x86 and x64 SDK packages
open command line and from the root directory check run: dotnet --version (at the time of writing it was 1.0.4)
fire up .net core project in VS 2017 and run.
When I did all of the above, I was able to start up the site in IIS Express from VS.

Resources