Generate an executable file from the local project .NET Core 3.1

table of Contents

Description: Compile managed code?

How it works: Principle

Applications

Sample Program

1. Check the installation of Visual Studio components

2. Add the repository has ILCompiler of NuGet

3. dependency to your project

4. Your application coding

The local generation program executable file

in conclusion


In this article, we will see a use .NET Core 3 sample program. We will review the process of local developers to generate an executable file its application and must be followed. We will look at how to check the Visual Studio installed components, has added ILCompiler of NuGet repository dependencies will be added to your project, write applications and generate local program's executable file.

Description: Compile managed code?

This feature is not integrated in the SDK but for the C there is provided a developer from # .NET Core 3 Original .csproj surprising method for generating machine-executable file. From my point of view, it is that you want to distribute software in a professional manner (this is a way :) key points. I found this feature in an excellent book (I did not forget to provide the source code for reference): Use .NET Core 3 and Visual Studio 2019 to develop multi-platform desktop applications.

With this tool, the .NET Core 3 and concurrent ( Python , the Node , the Java performance) match, to improve the development of performance-enhancing applications (Finally, C # almost reached the C ++ performance level).

How it works: Principle

.NET Core runtime managed projects for generating AOT (ahead of time) images. To build this code, pseudo-compiler MSIL conversion program to C ++ (this is regardless of which tags you use, and compile the results to build the application's executable file local OS requires C ++ reasons tools). One of its main developers ( in the Microsoft guys) Matt Warren  has been well documented this whole process .

To recover, the compiler generates a truly native executable file, and the IL assemblies at runtime the JIT ( the Just the In Time code compiler) compiled to.

Applications

Obvious benefits provided by using native executable file is ridiculous performance improvements, the lack of a mounting frame.

The most suitable program uses mainly local format desktop applications (service console or the GUI ), because for this type of application, local computing benefits is obvious.

On the other hand, for Web applications ( Web application and API ), due to the deployment of different types of use, benefits are not so obvious. In the Web server environment, in most cases, the frame has been installed on the computer, using a local executable manner that is not so obvious.

Sample Program

This example uses .NET Core3 . Let's review process for the developers to make their applications native executable files that must be followed.

1. Check the Visual Studio components installed

ILCompiler added to the required .NET Core 3 rear frame (as well as all the components required to use the framework of the development) is necessary to provide C ++ desktop functionality.

This is a must in your Visual Studio installed on the C ++ function:

2. Add has ILCompiler of NuGet repository

Dedicated NuGet repository contains the compiled executable file as a true local required Microsoft.Dotnet.ILCompiler dependencies.
The repository URL is:
HTTPS : //dotnetfeed.blob.core.windows.net/dotnet-core/index.json .

You must add it to NuGet source repository, if you are using Visual Studio , you must follow the images below 1 add it to the.

Picture 1

3. The dependency to your project

You can Figure 2 shown in the list of available packages Microsoft.Dotnet.ILCompiler to add. Do not forget to check ' include pre-release version " box (because the package is still labeled " Alpha " version).

Picture 2

4. your application coding

Example factorial console application execution, and display each level (calculated per step), and the final result is displayed in red ( Groovy !).

This is the source code:

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);
        }
    }
}

The generated program executable file local

Once the project debugging and testing, you now want to generate local application executable file. Said the .csproj file has to include a locally generated executable target platform identifier (running the RID of ).

<runtimeidentifier>win-x64</runtimeidentifier>

If you are using Visual Studio 2019 , you can use the " release " command (context menu item on the item) to generate a local executable file, as shown in 3 below.

Picture 3

If you prefer the command line, you can also use the CLI command " DOTNET publish " .

in conclusion

Generate executable file is a local .NET Core 3 significant advantages, you will benefit from C # high productivity, performance and use of almost the classic C ++ as good performance can be achieved.

As described herein, the ILCompiler library in Windows use on, but Linux and Mac OS X can also be used on.

Published 71 original articles · won praise 152 · views 520 000 +

Guess you like

Origin blog.csdn.net/mzl87/article/details/105058086