This article is a mirror article of machine translation, please click here to jump to the original article.

View: 33299|Reply: 2

[.NET Core] The new .NET Core features release a single-file executable

[Copy link]
Posted on 2019-9-28 16:38:47 | | | |
A few days ago, Microsoft released .NET Core 3.0, and one of the biggest enhancements is support for Windows desktop applications (Windows only). By using the .NET Core 3.0 SDK Windows Desktop Component, you can port Windows Forms and Windows Presentation Foundation (WPF) applications. To be clear, Windows desktop components are supported and included only on Windows.

SDK download address:The hyperlink login is visible.

Links to introduce .NET Core features:

The hyperlink login is visible.
The hyperlink login is visible.

Features include the following:

  • Use C# to build rich, interactive client web apps.
  • Use gRPC to create high-performance backend services.
  • SignalR now supports automatic reconnection and client-to-server flows.
  • Use OpenAPI documentation to generate strongly typed client code for Web APIs.
  • Endpoint routing is integrated through the framework.
  • Kestrel has HTTP/2 enabled by default.
  • Integrated web API and single-page app authentication support.
  • Certificates and Kerberos authentication are supported.
  • Integrate the new System.Text.Json serializer.
  • The new universal hosting sets up common managed services such as dependency injection, configuration, and logging.
  • A new Worker Service template for building long-term services.
  • New EventCounters are created for requests per second, total, current, and failed requests.
  • Startup errors hosted in IIS are now reported to the Windows Event Log.
  • Request pipeline integration System.IO.Pipelines.
  • Performance improvements across the entire technology stack.

Let's create a new .NET Core 3.0 console project with VS 2019, as shown in the figure below:



Referencing the Newtonsoft.Json package, write a few lines of code, as follows:


We right-click on VS Regenerate, then open the bin directory of the project, and find that several files will be generated, including :demo1.exe and Newtonsoft.Json.dll files.



Publish a single-file executable

Method 1:

Run the following command in the project directory:



We open the C:\Users\itsvse_pc\source\repos\demo1\demo1\bin\Debug\netcoreapp3.0\win10-x64 folder, and we will find that many dll files are generated and there is a publish folder.

Go to the publish folder,The size of the viewing demo1.exe is 66M, which is the same size as all files outside the folder, as shown in the figure below:



The generated single executable file actually contains the environment required for the execution of the program, and does not require the target computer to install the SDK, and the disadvantage is that the file size will become very large.

Method 2:

Right-click vs edit the project file and add the PublishSingleFile node configuration, the code is as follows:

Then right-click and click the "Publish" button.


Assembly links

The .NET core 3.0 SDK comes with a tool that can reduce the size of your app by analyzing IL and clipping unused assemblies.

Self-contained apps include everything you need to run code without having to install .NET on your main computer. However, many times the app only needs a fraction of the framework to run, and other unused libraries can be removed.

.NET Core now includes a setting that scans your app's IL using the IL linker tool. This tool will detect which code is required and then clip unused libraries. This tool can significantly reduce the deployment size of some applications.

To enable this tool, use the <PublishTrimmed> settings in your project and publish a self-contained app:

.NET Core CLI


We just generated a 66M file, and after adding the configuration,It became 35M, reducing the occupancy of 30M

Be sure to consider applications or frameworks that use reflections or related dynamic features (including ASP.NET Core and WPF)Usually damaged during cuttingThis corruption occurs because the linker is unaware of this dynamic behavior and cannot determine which frame types are required for reflection。 The IL linker tool can be configured to spot this situation.

Most importantly, be sure to apply it after cuttingConduct tests


(End)






Previous:Solve ajax not being able to assign values to external variables
Next:C language is so powerful, what language is it written in?
Posted on 2019-9-29 09:30:44 |

Thank you for sharing
 Landlord| Posted on 2019-10-30 09:14:15 |
ReadyToRun image
It can be improved by compiling the application set into the ReadyToRun (R2R) format. NET Core application startup time.

R2R is a form of pre-compiled (AOT). R2R binaries improve startup performance by reducing the amount of work that real-time (JIT) compilers need to perform when the application loads. The binary contains native code similar to what JIT will generate. However, R2R binaries are larger because they contain intermediate language (IL) code (which is still required in some cases) and a native version of the same code. R2R is available only when you publish a self-contained app that targets a specific runtime environment (RID), such as Linux x64 or Windows x64.



Here we first leave the comment and <PublishTrimmed>then add<PublishReadyToRun>:

Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com