从.NET Core 3.1项目生成本机可执行文件

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.

在本文中,我们将看到一个使用.NET Core 3的示例程序。我们将回顾开发人员为生成其应用程序的本机可执行文件而必须遵循的过程。 我们将看一下如何检查Visual Studio安装的组件,添加具有ILCompiler的NuGet存储库,将依赖项添加到您的项目,编写应用程序并生成程序的本机可执行文件。

简介:托管代码的编译? (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):

该功能未集成在SDK中,但为C#开发人员提供了一种惊人的方式,可以从.NET Core 3原始.csproj生成机器可执行文件。 从我的角度来看,这是您要以专业方式分发软件(这是:)的关键点。 我在一本出色的书中发现了此功能(我没有忘记提及出处,以供参考):

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

使用.NET Core 3和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).

使用此工具,.NET Core 3在开发性能增强的应用程序方面胜过并发(Python,Node,Java)(最后,C#几乎达到了C ++性能级别)。

工作原理:原理 (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.

.NET Core运行时用于生成托管项目的AOT(提前)图像。 为了构建该代码,伪编译器将MSIL程序转换为C ++代码,并编译结果以生成应用程序的本机可执行文件(这就是无论您使用哪种OS都需要C ++工具的原因)。 它的主要开发人员之一(Microsoft伙计)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).

为了进行恢复,与IL程序集相​​比,ILIT程序集在运行时由JIT(Just In Time编译器)编译为本地代码,因此,编译器会生成一个真正的本地可执行文件。

应用领域 (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.

最适合使用本机格式的程序主要是台式机应用程序(服务,控制台或GUI),因为对于这种类型的应用程序,本地计算的收益非常明显。

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.

另一方面,对于Web应用程序(Web应用程序和API),由于它们使用的部署类型,收益并不那么明显。 在Web服务器环境中,大多数情况下,该框架已安装在计算机上,使用本机可执行文件的方式不那么明显。

样例程序 (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.

该示例程序使用.NET Core3。让我们回顾一下开发人员为制作其应用程序的本机可执行文件而必须遵循的过程。

1.检查Visual Studio安装的组件 (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.

ILCompiler添加到必需的.NET Core 3框架(以及使用该框架进行开发所需的所有组件)后,需要设置C ++桌面功能。

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

这是您必须在Visual Studio上安装的C ++功能:

Image 2

2.添加具有ILCompiler的NuGet存储库 (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.

专用的NuGet存储库包含将程序编译为真实的本机可执行文件所需的Microsoft.Dotnet.ILCompiler依赖项。 该存储库URL为: 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.

您必须将其添加到NuGet源存储库中,如果使用Visual Studio,则必须按下面的图片1所述添加它。

Image 3
Picture 1
图片1

3.将依赖项添加到您的项目中 (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).

您可以通过如图2所示,在可用软件包列表的列表中选择Microsoft.Dotnet.ILCompiler来添加它。不要忘记选中“ Include prerelease ”复选框(因为该软件包仍被标记为“ alpha ”版本)。

Image 4
Picture 2
图片2

4.为您的应用程序编码 (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!).

示例控制台应用程序执行阶乘计算,并显示每个等级(计算的每个步骤),并以红色显示最终结果(groovy!)。

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

5.生成程序的本机可执行文件 (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).

在对项目进行调试和测试后,您现在想要生成应用程序的本机可执行文件。 .csproj文件必须包括用于生成本机可执行文件的目标平台,运行时标识符(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.

如果使用Visual Studio 2019,则可以使用“ 发布 ”命令(项目项上的上下文菜单)来生成本机可执行文件,如图3所示。

Image 5
Picture 3
图片3

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

如果您更喜欢命令行,也可以使用CLI命令“ dotnet publish ”。

结论 (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++.

生成本机可执行文件是.NET Core 3的显着优势,您将受益于C#的高生产率,其性能几乎与使用经典C ++所能达到的性能一样好。

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

如本文所述, ILCompiler库可以在Windows上使用,但在Linux和Mac OS X上也可以使用。

翻译自: https://www.codeproject.com/Articles/5262251/Generate-Native-Executable-from-NET-Core-3-1-Proje

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值