Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / XML

Generate Native Executable from .NET Core 3.1 Project

Rate me:
Please Sign up or sign in to vote.
5.00/5 (20 votes)
28 Apr 2020CPOL4 min read 53.8K   563   29   29
Make true native executable (no framework required) from .NET Core 3.1 project (factorial calculation project example) with the CoreRT library

Image 1

In this article, we will see a sample program that uses .NET Core 3. We will review the process the developer will have to follow for producing native executable of his application. We will take a look at how to check the Visual Studio Installed Components, add the NuGet Repository which has the ILCompiler, add the dependency to your project, code your application and generate the native executable of the program.

Introduction: Compilation of a Managed Code?

The feature is not integrated in the SDK but provides to the C# developer an amazing way to produce machine executable from a .NET Core 3 original .csproj. From my point of view, this is a critical point when you want to distribute your software in a professional way (this is the way :). I discovered this feature in a stellar book (I didn’t forget to mention the source, for reference):

Developing multi-platform desktop applications with .NET Core 3 and Visual Studio 2019.

With this tool, the .NET Core 3 outmatches the concurrencies (Python, Node, Java) for the development of performance enhanced application (at last, C# almost reached the C++ performances level).

How It Works: The Principle

The .NET Core Runtime is used for generating AOT (Ahead of Time) image of the managed project. For building that, the pseudo compiler converts the MSIL program into C++ code and compiles the result for producing a native executable of the application (That’s why the C++ tools are required whatever the OS you are working on).
This complete process has been very well documented by one of its main developers (a Microsoft guy), Matt Warren.

For resuming, the compiler produces a real native executable, compared to an IL assembly which is compiled at run time to native code by the JIT (Just In Time compiler).

Fields of Application

The obvious benefits provided by using native executable are ridiculous performances improvement, the lack of framework to install.

The most adapted program for using the native format are mainly desktop applications (service, console or GUI) because the gain for local computing is quite noticeable for this type of application.

On the other hand, for web applications (Web app and API), the gains are not so obvious because of the kind of deployment they use. In a web server environment, most of the time, the framework is already installed on the machine and the use of a native executable is less manifest.

Sample Program

The sample program uses .NET Core 3. Let's review the process the developer will have to follow for making the jump in producing native executable of his application.

1. Check the Visual Studio Installed Components

Added to the required .NET Core 3 framework (and all the components required for the development with this framework), the ILCompiler required the setup of the C++ desktop feature.

Here is the C++ feature you have to install on Visual Studio:

Image 2

2. Add the NuGet Repository which has the ILCompiler

A dedicated NuGet repository contains the Microsoft.Dotnet.ILCompiler dependency required for the compilation of your program into real native executable.
This repository URL is:
https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json.

You have to add it to your NuGet source repos, if you use Visual Studio, you have to add it as described in picture 1 below.

Image 3

Picture 1

3. Add the Dependency to Your Project

You can add Microsoft.Dotnet.ILCompiler, by selecting it in the list of the available packages list as described in picture 2. Do not forget to check the box “Include prerelease” (because the package is still labelled “alpha” version).

Image 4

Picture 2

4. Code Your Application

The sample console application does a factorial calculation with display of every rank (each step of the calculation) and displays the final result in red (groovy!).

Here is the source code:

C#
using System;

namespace Net3Factorial
{
    class Program
    {
        static long Factorial(int x)
        {
            long result = 1;
            Console.ForegroundColor = ConsoleColor.Blue;
            for (int i = 1; i <= x; i++)
            {
                result = result * i;
                Console.WriteLine("Step {0}: {1}", i, result);
            }
            return result;
        }

        static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Clear();

            Console.WriteLine("Enter the Integer for the factorial calculation:");

            int num = Convert.ToInt32(Console.ReadLine());
       
            long f = Factorial(num);
            Console.ForegroundColor = ConsoleColor.Red;

            Console.Write("The Factorial of {0} is: {1}\n", num, f);
        }
    }
}

5. Generate the Native Executable of the Program

Once your project has been debugged and tested, you now want to produce the native executable of your application. The .csproj file has to include the target platform for the generation of the native executable, the runtime identifier (rid).

<runtimeidentifier>win-x64</runtimeidentifier>

If you use Visual Studio 2019, you can use the “Publish” command (context menu on the project item) for producing the native executable as shown in picture 3.

Image 5

Picture 3

You can also use the CLI command "dotnet publish" if you prefer the command line.

Conclusion

The generation of a native executable is a significant advantage for .NET Core 3, you benefit from the high productivity of C# and the performances are almost as good as the ones you could achieve using classic C++.

The ILCompiler library can be used on Windows as described in this article, but on Linux and Mac OS X as well.

History

  • 16th March, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) --
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNot working with .Net Core 3.1 and Microsoft.DotNet.ILCompiler 7.0.2 Pin
pwrgreg00710-Jan-23 11:12
pwrgreg00710-Jan-23 11:12 
AnswerRe: Not working with .Net Core 3.1 and Microsoft.DotNet.ILCompiler 7.0.2 Pin
maxoptimus17-Jul-23 10:30
maxoptimus17-Jul-23 10:30 
QuestionX86 support? Pin
Member 1572341331-Jul-22 17:53
Member 1572341331-Jul-22 17:53 
GeneralHere another similar link Pin
Gaamaa Resosa14-Oct-20 13:56
Gaamaa Resosa14-Oct-20 13:56 
QuestionFirst time Microsoft gave us a Good News after several years of .NET security ! Pin
Gaamaa Resosa14-Oct-20 13:55
Gaamaa Resosa14-Oct-20 13:55 
Generallooks like old feature but without your good sample ms docs is terrible Pin
maxoptimus4-Oct-20 4:45
maxoptimus4-Oct-20 4:45 
Praisenice job; Pin
maxoptimus4-Oct-20 4:31
maxoptimus4-Oct-20 4:31 
QuestionIt just moves the .NET code to a dll. Pin
Peter C. Matthews14-Sep-20 17:16
professionalPeter C. Matthews14-Sep-20 17:16 
AnswerRe: It just moves the .NET code to a dll. Pin
maxoptimus4-Oct-20 4:45
maxoptimus4-Oct-20 4:45 
AnswerRe: It just moves the .NET code to a dll. Pin
maxoptimus4-Oct-20 4:38
maxoptimus4-Oct-20 4:38 
QuestionCould I use for windows form project on net core? Pin
Member 1411097915-Jul-20 0:01
Member 1411097915-Jul-20 0:01 
QuestionMicrosoft.Dotnet.ILCompiler package not found in pre-release Pin
Member 1099862630-Jun-20 23:24
Member 1099862630-Jun-20 23:24 
Questionexcellent Pin
amigoface23-Jun-20 1:15
amigoface23-Jun-20 1:15 
does this work also with a full .net core 3.x WPF applications ?
QuestionWow, really awesome info!! Pin
Pinox_8-May-20 21:19
Pinox_8-May-20 21:19 
QuestionGreat thing! Another usage is ... Pin
LightTempler29-Apr-20 11:10
LightTempler29-Apr-20 11:10 
QuestionIt works!!! Pin
koder428-Apr-20 12:28
koder428-Apr-20 12:28 
QuestionIt doesn't work Pin
Member 1464259519-Apr-20 12:37
Member 1464259519-Apr-20 12:37 
AnswerRe: It doesn't work Pin
Ray Porter28-Apr-20 9:26
Ray Porter28-Apr-20 9:26 
AnswerRe: It doesn't work Pin
maxoptimus4-Oct-20 4:28
maxoptimus4-Oct-20 4:28 
QuestionAwesome but ... Pin
Dean Goddard1-Apr-20 1:56
Dean Goddard1-Apr-20 1:56 
AnswerRe: Awesome but ... Pin
Ray Porter5-Apr-20 4:29
Ray Porter5-Apr-20 4:29 
PraiseRe: Awesome but ... Pin
Dean Goddard6-Apr-20 13:30
Dean Goddard6-Apr-20 13:30 
QuestionA Question Pin
Randy Ledyard18-Mar-20 4:11
Randy Ledyard18-Mar-20 4:11 
AnswerRe: A Question Pin
Ray Porter5-Apr-20 4:31
Ray Porter5-Apr-20 4:31 
QuestionPromissor Pin
Jefferson S. Motta17-Mar-20 13:04
Jefferson S. Motta17-Mar-20 13:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.