. net core single file publishing static compilation AOT corert method details

Time:2019-11-22

Preface

This article mainly introduces the static compilation of AOT corert for. Net core single file publishing, which packages the. Net core application into an executable file and contains the runtime.

Support windows, MacOS and Linux x64 w / ryujit CodeGen.

Sample project:

https://github.com/dotnet/corert/tree/master/samples/WebApi

Here’s the actual experience.

First, make sure to install the C + + compilation environment and the SDK of. Net core 2.0 and above.

New webapi app

Open the command prompt and enter the following:


dotnet new webapi -o zeroapi

cd zeroapi

Create a new webapi app.

Add corert to project

At present, corert is also alpha version, and there are packages on myget.

First add a nuget.config


dotnet new nuget

Then in<packageSources> The nodes are added as follows:


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

Next, add the microsoft.dotnet.ilcompiler package reference:


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

Replace default services and handle reflection

Open startup.cs, andservices.AddMvc(); replace withservices.AddMvcCore().AddJsonFormatters();

Then create a rd.xml configuration file for the reflection processing runtime to ensure that the reflection assembly is compiled into the program.

Rd.xml configuration file is as follows:


<Directives>
 <Application>
 <Assembly Name="zeroapi" Dynamic="Required All" />
 <Assembly Name="Microsoft.AspNetCore.Server.Kestrel.Core">
  <Type Name="Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServer" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Server.Kestrel.Core.Internal.KestrelServerOptionsSetup" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.AspNetCore.Server.Kestrel" Dynamic="Required All"/>
 <Assembly Name="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv">
  <Type Name="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.LibuvTransportFactory" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.LibuvTransportOptions" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.DependencyInjection" Dynamic="Required All">
  <Type Name="Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteExpressionBuilder" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteValidator" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.Options">
  <Type Name="Microsoft.Extensions.Options.OptionsManager`1[[Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions,Microsoft.AspNetCore.Server.Kestrel.Core]]" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.Options.OptionsFactory`1[[Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions,Microsoft.AspNetCore.Server.Kestrel.Core]]" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.Options.OptionsMonitor`1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions,Microsoft.Extensions.Logging.Console]]" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.AspNetCore.Mvc.Core" Dynamic="Required All" />
 <Assembly Name="Microsoft.AspNetCore.Routing">
  <Type Name="Microsoft.AspNetCore.Routing.Internal.RoutingMarkerService" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Builder.RouterMiddleware" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Routing.Tree.TreeRouteBuilder" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Routing.DefaultInlineConstraintResolver" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Routing.RouteOptions" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.AspNetCore.Mvc.Formatters.Json">
  <Type Name="Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.MvcJsonMvcOptionsSetup" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Mvc.MvcJsonOptions" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.AspNetCore.Authorization">
  <Type Name="Microsoft.AspNetCore.Authorization.DefaultAuthorizationPolicyProvider" Dynamic="Required All" />
  <Type Name="Microsoft.AspNetCore.Authorization.AuthorizationOptions" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.AspNetCore.Http">
  <Type Name="Microsoft.AspNetCore.Http.HttpContextFactory" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.AspNetCore.Hosting" Dynamic="Required All">
  <Type Name="Microsoft.AspNetCore.Hosting.Internal.ApplicationLifetime" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.Logging.Abstractions">
  <Type Name="Microsoft.Extensions.Logging.Logger`1[[Microsoft.AspNetCore.Hosting.Internal.WebHost,Microsoft.AspNetCore.Hosting]]" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.Logging">
  <Type Name="Microsoft.Extensions.Logging.LoggerFactory" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.Logging.Console">
  <Type Name="Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.Logging.Console.ConsoleLoggerProvider" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.Logging.Debug">
  <Type Name="Microsoft.Extensions.Logging.Debug.DebugLogger" Dynamic="Required All" />
  <Type Name="Microsoft.Extensions.Logging.Debug.DebugLoggerProvider" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="System.Linq.Expressions">
  <Type Name="System.Linq.Expressions.ExpressionCreator`1[[Newtonsoft.Json.Serialization.ObjectConstructor`1[[System.Object,System.Private.CoreLib]],Newtonsoft.Json]]" Dynamic="Required All" />
  <Type Name="System.Linq.Expressions.ExpressionCreator`1[[System.Func`2[[System.Object,System.Private.CoreLib],[System.Object,System.Private.CoreLib]],System.Private.CoreLib]]" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.ObjectPool">
  <Type Name="Microsoft.Extensions.ObjectPool.DefaultObjectPoolProvider" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Newtonsoft.Json">
  <Type Name="Newtonsoft.Json.Serialization.ObjectConstructor`1[[System.Object,System.Private.CoreLib]]" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="System.ComponentModel.TypeConverter">
  <Type Name="System.ComponentModel.TypeConverter" Dynamic="Required All" />
  <Type Name="System.ComponentModel.StringConverter" Dynamic="Required All" />
  <Type Name="System.ComponentModel.Int32Converter" Dynamic="Required All" />
 </Assembly>
 <Assembly Name="Microsoft.Extensions.Configuration.Json">
  <Type Name="Microsoft.Extensions.Configuration.Json.JsonConfigurationSource" Dynamic="Required All" />
 </Assembly>
 </Application>
</Directives>

If you want to replace it with your own program, change<Assembly Name="zeroapi" Dynamic="Required All" />Name in.

Open zeroapi.csproj

stay<PropertyGroup>Under node<RdXmlFile>rd.xml</RdXmlFile>Join.

Next will <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />Replace with the following:


<PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.0.1" />

Next, change the valuescontroller to ensure the function is normal, as follows:


public class ValuesController
 {
 [HttpGet("/")]
 public string Hello() => "Hello World! LineZero AOT";
 // GET api/values
 [HttpGet("/api/values")]
 public IEnumerable<string> 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 publish

Before publishing, use dotnet run to make sure the function is working properly.

For publishing, open x64 native tools command prompt for vs 2017, and be sure to publish on this command line. Find visual studio 2017 in the start menu and open to find it.

Issue command ordotnet publish -r <RID> -c <Configuration>

Windows 64 bit is published heredotnet publish -r win-x64 -c releaseFor the first time, the corresponding package will be restored by default, which will take some time.

After publishing, open the bin \ x64 \ release \ netcoreapp2.0 \ win-x64 \ publish folder, where zeroapi.exe is the final file, which can be executed.

It can be seen that the final size is about 21m. Then visit http: / / localhost: 5000 / as shown:

Console demo

Create a new console application and publish it using AOT.

There is no reflected content and no need for rd.xml file.

The code is as follows:


class Program
 {
 static void Main(string[] args)
 {
  Console.WriteLine("Hello World!");
  Console.WriteLine("LineZero AOT Demo!");
  Console.ReadKey();
 }
 }

Finaldotnet publish -r win-x64 -c release Release

The final single file size is less than 4m!

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

GitHub:https://github.com/dotnet/corert

summary

The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.