Is there a way to make a console application run using only a single file in .NET Core?

Viewed 6823

In .NET framework, you can make a single .EXE file that will run from the command line without having any extra config files (and if using ILMerge, you can put all .DLL references into the 1 .EXE assembly).

I am taking a stab at using .NET Core to accomplish the same thing, but so far without success. Even the simplest Hello World application with no dependencies requires there to be a file named <MyApp>.runtimeconfig.json in order to run using dotnet.exe.

dotnet F:\temp\MyApp.dll

The contents of the <MyApp>.runtimeconfig.json are as follows:

{
  "runtimeOptions": {
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "1.1.1"
    }
  }
}

Without this config file in the same folder as the .DLL, I get the following error:

A fatal error was encountered. The library 'hostpolicy.dll' required to
execute the application was not found in 'F:\temp'.

My question is: Is there some way to change the application so it doesn't require this config file to be present, so that the defaults of this information are compiled within the .DLL but can be overridden by adding the config file?

NOTE: I also want to ensure it "just works" regardless of the platform it is installed on it provided the platform has the right version of .NET Core.

Background

I am trying to get a smooth user experience for running some utilities that are useful sometimes, but are rarely ever needed. Since it doesn't appear to be possible to use the same .DLL that is referenced from a client application as a console application, the next best thing would be to have a single file that could be downloaded and run without any dependencies.

For example, in Java you can simply download a .jar file on any supported platform and run:

java <package>.jar <namespace>.SomeClass [args]

and it will "just work" without any extra files. How can I get a similar user experience using .NET Core?

In a nutshell, I want to try to avoid the extra step of "unzip to a directory first"...

3 Answers

Tested with .NET Core 2.2 on a console app:

  1. Reference Microsoft.DotNet.ILCompiler package in your output project. You'll need to add MyGet package repository in Visual Studio settings. *
  2. Publish the project via command line, dotnet publish C:\src\App\App.csproj -c release -r win-x64 -o output-win-x64. If there's no "Desktop Development for C++" component installed, do it in Visual Studio Installer, or the command will fail.
  3. Go to the output folder (e.g. "C:\src\App\output-win-x64") and grab the native image (.exe file).

On Windows it produced a fully functional 5Mb .exe file (compared to original self-contained publish with folder size at ~60Mb). On macOS the ILComplier though produced output without any error, the app crashed with unhandled expection (on the line with LINQ expression).

*Go to "Tools -> Options -> Package Manager -> Package Sources" and add new source at https://dotnet.myget.org/F/dotnet-core/api/v3/index.json

It is possible in .NET Core 3.0+

The feature is enabled by the usage of the following property in your project file (.csproj):

<PropertyGroup>
    <PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>

There are other options as well, such as packaging the pdb into the bundle, or leaving certain files out.

Documentation can be found here: https://docs.microsoft.com/en-us/dotnet/core/deploying/#publish-self-contained

True it just works:

Combine this technique with the Self Contained Deployment workflow, you can get a true "it just works" experience for your user, they don't even have to install the .NET Core runtime for your app to run.

I am currently deploying applications to my clients as single .exe files.

Read more about that here: https://docs.microsoft.com/en-us/dotnet/core/deploying/#self-contained-deployments-scd

Related