. NET Core single file released static compilation AOT CoreRT, aotcorert

Source: Internet
Author: User

. NET Core single file released static compilation AOT CoreRT, aotcorert
. NET Core single file released static compilation AOT CoreRT, package the. NET Core application into an executable file and include the runtime.

Supports Windows, MacOS and Linux x64 w/RyuJIT codegen.

The following describes the actual experience.

First, make sure to install the C ++ compiling environment and the. NET Core 2.0 or later SDK.

Create a WebAPI Application

Open the command prompt and enter the following:

Dotnet new webapi-o zeroapi

Cd zeroapi

Create a WebAPI application.

Add CoreRT to project

Currently, CoreRT is still in alpha version and there are packages on myget.

First add an nuget. config

Dotnet new nuget

Then Add the following node:

Add the Microsoft. DotNet. ILCompiler package reference:

Dotnet add package Microsoft. DotNet. ILCompiler-v 1.0.0-alpha -*

Replace default services and process reflection

Open Startup. cs and replace services. AddMvc (); with services. AddMvcCore (). AddJsonFormatters ();

Create an rd. xml configuration file to ensure that the reflection assembly is compiled into the program during reflection processing.

The rd. xml configuration file is as follows:

View Code

If you want to replace it with your own program, change the Name in.

Open zeroapi. csproj

In Under the node Rd. xml Join.

Then Replace it with the following:

Next, change ValuesController to make sure the function is normal, as shown below:

Public class ValuesController

{

[HttpGet ("/")]

Public string Hello () => "Hello World! LineZero AOT ";

// GET api/values

[HttpGet ("/api/values")]

Public IEnumerable Get ()

{

Return new string [] {"AOT", "CoreRT "};

}

// GET api/values/5

[HttpGet ("/api/values/{id}")]

Public string Get (int id)

{

Return "Your value is" + id;

}

}

The program transformation is completed, followed by the most important release.

Restore and release

Before the release, use dotnet run to ensure that the function runs properly.

For release, open x64 Native Tools Command Prompt for VS 2017. Note that this Command line must be released. In the Start Menu, find Visual Studio 2017.

Release command or dotnet publish-r -C

Here the release of Windows 64-bit dotnet publish-r win-x64-c release, the first will default to restore the corresponding package, it takes some time.

After the release is complete, open the bin \ x64 \ release \ netcoreapp2.0 \ win-x64 \ publish folder where zeroapi.exe is the final file and you can execute it.

The final size is about 21 MB. Then access https: // localhost: 5000/

Console Demo

Create a console application and use AOT to publish it.

Simple console, no reflected content, No rd. xml file required.

The Code is as follows:

Class Program

{

Static void Main (string [] args)

{

Console. WriteLine ("Hello World! ");

Console. WriteLine ("LineZero AOT Demo! ");

Console. ReadKey ();

}

}

Eventually dotnet publish-r win-x64-c release released

The size of a single file is less than 4 MB!

At present, this technology is still an early version and Microsoft is expected to bring it to the official version.

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.