A fatal error occurred. The required library hostfxr.dll could not be found
Further to Ajith's answer, "Deployment Mode: Self Contained" can also be selected in Visual Studio 2019 here:
I got the same error for my .Net core 3.0 app today. This is because you are missing the .net core run time in the machine, and is installing a framework dependent application.
The solution for this is to publish the application with Deployment Mode Self Contained.
Use the below command to publish from command line
dotnet publish -c Release -r <RID> --self-contained true
get the RID details from https://docs.microsoft.com/en-us/dotnet/core/rid-catalog#windows-rids
As from the error message, if this is a framework-dependent application :
If this is a framework-dependent application, install the runtime in the global location [C:\Program
Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or
register the runtime location in [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\InstallLocation].
Please set the DOTNET_ROOT environment to one of the following locations, depending upon where you have installed dotnet.exe
C:\Program Files\dotnet OR C:\Program Files (x86)\dotnet
from "My Computer | Properties | Advanced | Environment Variables". Now restart your IDE/Terminal whatever you are using.
My solution to avoid "hostfxr.dll missing" in a framework-dependent application was to install the the "Hosting Bundle" on the target computer.
The ASP.NET Core Runtime enables you to run existing web/server applications. On Windows, we recommended installing the Hosting Bundle, which includes the .NET Core Runtime and IIS support.
https://dotnet.microsoft.com/download/dotnet-core/thank-you/runtime-aspnetcore-3.1.9-windows-hosting-bundle-installer
I ended up on this page many times in my search for a solution to my problem.
My self contained exe was raising the error
Could not load file or assembly 'System.Data.SqlClient, Version=4.6.1.1
I was publishing the .net core 3.1 runtime and referencing a netstandard library that referenced System.Data.SqlClient, Version=4.6.1.1
The following git hub page has this marked as a known issue https://github.com/dotnet/core/blob/master/release-notes/3.1/3.1-known-issues.md#net-core-312
Setting our exe to publish the .net core 3.0 runtime fixed this for us. We have control of the referenced library so will probably follow the advice on the github page but for anyone else that has not got control of library may find this of use.