.NET Core单文件发布静态编译AOT CoreRT的方法详解
前言
本文主要介绍的是关于.net core单文件发布静态编译aot corert,将.net core应用打包成一个可执行文件并包含运行时。
支持windows, macos and linux x64 w/ ryujit codegen。
示例项目:
https://github.com/dotnet/corert/tree/master/samples/webapi
下面来实际体验。
首先确保安装c++编译环境,以及.net core 2.0 以上版本sdk。
新建webapi应用
打开命令提示符,输入如下:
dotnet new webapi -o zeroapi cd zeroapi
新建webapi应用。
添加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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。