.NET Core單文件發佈靜態編譯AOT CoreRT,將.NET Core應用打包成一個可執行文件幷包含運行時。html
支持Windows, MacOS and Linux x64 w/ RyuJIT codegen。git
示例項目:github
https://github.com/dotnet/corert/tree/master/samples/WebApiweb
下面來實際體驗。json
首先確保安裝C++編譯環境,以及.NET Core 2.0 以上版本SDK。api
新建WebAPI應用
打開命令提示符,輸入以下:併發
dotnet new webapi -o zeroapiapp
cd zeroapiasp.net
新建WebAPI應用。ide
添加CoreRT到項目
目前CoreRT仍是alpha版,在myget上有包。
首先添加一個nuget.config
dotnet new nuget
而後在 <packageSources> 節點添加以下:
<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" />
接着添加Microsoft.DotNet.ILCompiler包引用:
dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
替換默認services並處理反射
打開Startup.cs ,將 services.AddMvc(); 替換成 services.AddMvcCore().AddJsonFormatters();
接着建立一個rd.xml 配置文件,用於反射處理運行時,確保反射程序集編譯到程序中。
rd.xml配置文件以下:

<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>
其中若是你要替換成本身的程序,更改 <Assembly Name="zeroapi" Dynamic="Required All" /> 中的Name。
打開zeroapi.csproj
在<PropertyGroup>節點下將 <RdXmlFile>rd.xml</RdXmlFile> 加入。
接着將 <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" /> 替換成以下:
<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" />
接着將 ValuesController 更改一下,確保功能正常,以下:
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; } }
程序改造完成,接着最重要的發佈。
還原併發布
發佈以前,使用dotnet run 確保功能正常運行。
發佈請打開 x64 Native Tools Command Prompt for VS 2017 ,注意必定在此命令行發佈。在開始菜單找到 Visual Studio 2017,打開就能找到。
發佈命令仍是 dotnet publish -r <RID> -c <Configuration>
這裏發佈Windows 64位 dotnet publish -r win-x64 -c release,初次的話會默認還原對應的包,須要一些時間。
發佈完成後,打開bin\x64\release\netcoreapp2.0\win-x64\publish 文件夾,裏面zeroapi.exe即爲最終文件,執行便可。
能夠看到最終大小爲21m左右。而後訪問http://localhost:5000/ 如圖:
控制檯Demo
新建一個控制檯應用,使用AOT發佈。
單純控制檯,沒有反射的內容,無須要rd.xml 文件。
代碼以下:
class Program { static void Main(string[] args) { Console.WriteLine("Hello World!"); Console.WriteLine("LineZero AOT Demo!"); Console.ReadKey(); } }
最終 dotnet publish -r win-x64 -c release 發佈
最終單個文件大小不到4m!
目前該技術仍是早期版本,期待微軟最終帶到正式版。
GitHub:https://github.com/dotnet/corert