How can we quickly achieve independent cross-platform release of the. NET Core program,

Source: Internet
Author: User

How can we quickly achieve independent cross-platform release of the. NET Core program,
. NET Framework (Part)

From. since the birth of NET Framework 1.0, a problem that may have been asked millions of times has emerged. "I wrote. NET program can be directly deployed without dependency. what about NET Framework?" So is there a possibility?

You may hear a lot of answers, such:

  • "No !" This answer is not surprising for beginners or senior people, because they have different ways of thinking. Beginners do not know what else is there, and senior people may be too reluctant to explain a lot to you.

  • "Package the. NET Framework installation package !" The person who can give you the standard answer, I believe, has already crossed this river. If you have any technical questions, please remember to ask him/her.

  • "Use Apsara Virtual Machine !" People who give such answers are probably the hidden gods around you. There are too many stories behind this term, but it is not the focus of this article. If you are interested, you can leave a message after the text. If there are enough people, I don't mind writing any more.

For most individual users, installing a. NET Framework installation package with a size of dozens of megabytes and a size of hundreds of megabytes is a nightmare in the past few years. Not to mention bandwidth limitations such as network downloads. Even hard disk space and disc space are reserved for other important data (slice and video). Where can we hold the next irrelevant runtime.

Therefore, the. NET program seems to have been designed for the enterprise from the very beginning. Only they can install such things on the machine to ensure that your program can run smoothly. Independent developers, or software development companies for individual users, may put. NET aside from technology selection.

I have to say that Java is much more clever in this regard. By inserting a JVM in the program that is not actually small, you can be unaware (such as Eclipse built-in JVM ).

You should not think that it is just a good time for Microsoft to design it. First of all,. NET Framework has become a time bomb, a security patch, a lifecycle, or something on Windows systems. In fact, Microsoft does not think it is very easy. Since Windows Server 2003 has built-in. NET Framework 1.1, Microsoft is also a nightmare. Second. NET Framework soon has 2.0/3.0/3.5/4.0/4.5/4.5.x/4.6.x/4.7.x and so on. The installation and uninstallation may cause instability, SO 4. x and above are replaced with the original installation. Yes, in fact, this is why Microsoft saves itself trouble, although the customer is not happy at all. If you calculate the parallel execution of the Office plug-in system and the. NET Program of different IIS framework versions (side by side), I will go, and Microsoft will soon blow up. IIS is better. It can force each application pool to only use a specific version of the. NET Framework, while Office needs to load plug-ins of multiple Framework versions at the same time, OMG, in fact, it is troublesome to survive.

Here, you won't be surprised how to get to the new platform. NET Core. Microsoft recommends the self-contained mode. So how can this model be used?

. NET Core released independently

In fact, this part is really simple... It cannot be simple. After installing the. NET Core SDK, find an empty directory and try the following command,

Dotnet new console

Dotnet publish-c Release -- self-contained

OK. Are you stuck?

/Usr/local/share/dotnet/sdk/2.0.2/Sdks/Microsoft. NET. sdk/build/Microsoft.NET.RuntimeIdentifierInference.tar gets (116,5): error: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. please either specify a RuntimeIdentifier or set SelfContained to false. [/Users/lextm/Projects/selfcontained. csproj]

Don't be surprised at all. Microsoft reminds you that you still need to provide something, otherwise this deployment task cannot be implemented.

So in this case, we have to open the project file to see,

<Project Sdk = "Microsoft. NET. Sdk">

<PropertyGroup>

<OutputType> Exe </OutputType>

<TargetFramework> netcoreapp2.0 </TargetFramework>

</PropertyGroup>

</Project>

OK, try to change it to this,

<Project Sdk = "Microsoft. NET. Sdk">

<PropertyGroup>

<OutputType> Exe </OutputType>

<TargetFramework> netcoreapp2.0 </TargetFramework>

<RuntimeIdentifiers> win-x86; win-x64; linux-x64 </RuntimeIdentifiers>

</PropertyGroup>

</Project>

This line specifies all operating system platforms supported by this program, which are Windows 32-bit, Windows 64-bit, Linux 64-bit, and macOS 64-bit. For more information about descriptors, see the official documentation.

Next, return to the command line to continue the release,

Dotnet publish-c Release -- self-contained-r win-x64

Microsoft (R) Build Engine version 15.4.8.50001 for. NET Core

Copyright (C) Microsoft Corporation. All rights reserved.


Selfcontained->/Users/lextm/Projects/selfcontained/bin/Release/netcoreapp2.0/win-x64/selfcontained. dll

Selfcontained->/Users/lextm/Projects/selfcontained/bin/Release/netcoreapp2.0/win-x64/publish/

In this way, the program deployment file required for Windows 64-bit is ready. (Do not be surprised with the path separator above, because I am releasing Windows programs on Mac .)

Open the publish folder and let's take a look at what Microsoft has done.

Mysterious release directory

++, Just a TMD Hello Kitty... World Program,It's hard to be stuffed with 216 files.!!! (Does your chrysanthemum hurt ?) However, the folder is copied to the Windows machine and then double-click selfcontained.exe. (What should I do? It's amazing. I can only forgive it .)

The-r switch is used to specify the target platform. If you want to publish data to multiple platforms, You can execute dotnet publish multiple times and replace the descriptor after-r each time.

Secret:. net il Linker

Unfortunately, not all programs are as slim as the above example.

I will ask you if you are too big.

Success! I will ask you if it is too big. A Hello World is over 60 MB.

Although reluctant, Microsoft can only admit that this is too disappointing, so a temporary solution is provided, namely. net il Linker. The usage method is not complicated at all (not really ...).

Return to the project root directory and run

Dotnet new nuget

In this way, an nuget. config file will be created,

<? Xml version = "1.0" encoding = "UTF-8"?>

<Configuration>

<PackageSources>

<! -- To inherit the global NuGet package sources remove the <clear/> line below -->

<Clear/>

</PackageSources>

</Configuration>

Modify it,

<? Xml version = "1.0" encoding = "UTF-8"?>

<Configuration>

<PackageSources>

<Add key = "dotnet-core" value = "https://dotnet.myget.org/F/dotnet-core/api/v3/index.json"/>

</PackageSources>

</Configuration>

In this way, we can connect to Microsoft's NuGet source for testing. Continue typing in the command line,

Dotnet add package ILLink. Tasks-v 0.1.4-preview-981901

Okay, so that IL Linker can be installed. Try dotnet publish again, for example

Dotnet publish-c Release -- self-contained-r win-x64/p: ShowLinkerSizeComparison = true

(Note that the/p section only shows you the effect of downgrading. It is not necessary .)

How are you satisfied? This time there are only over 30 megabytes left.

About the future

From the early days of. NET Core, friends who have been paying attention to it will never forget Microsoft's commitment to "one file" release method. I have to say that Microsoft has put the satellite early. It doesn't mean that Microsoft cannot deploy the next file for you now, but it does not mean that, once you are so busy, you will have any debugging experience, optimization, or anything else. Then you should turn back and scold Microsoft TMD as a big liar.

So wait patiently. The deployment method of a file should at least wait until CoreRT and. NET Native are completely completed. However, from the Progress point of view, there is still a point in the future... Sad...

Conclusion

Recently, I have received some private emails from my friends asking questions. Sorry, I cannot answer any complicated questions except one or two answers. For convenience, go to professional forums such as Stack Overflow for help.

If you really need to ask me to do the consulting service, the price is also listed below, and Tong Yi is not bullied.

Thank you for reading.

Original address: https://weibo.com/ttarticle/p/show? Id = 2309404189811899529202

 

. NET community news, deep good article, welcome to visit the public number article summary http://www.csharpkit.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.