溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

.NET Core如何實(shí)現(xiàn)單文件發(fā)布靜態(tài)編譯AOT CoreRT

發(fā)布時(shí)間:2021-08-07 11:36:07 來(lái)源:億速云 閱讀:127 作者:小新 欄目:開(kāi)發(fā)技術(shù)

小編給大家分享一下.NET Core如何實(shí)現(xiàn)單文件發(fā)布靜態(tài)編譯AOT CoreRT,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

首先確保安裝C++編譯環(huán)境,以及.NET Core 2.0 以上版本SDK。

新建WebAPI應(yīng)用

打開(kāi)命令提示符,輸入如下:

dotnet new webapi -o zeroapi

cd zeroapi

新建WebAPI應(yīng)用。

添加CoreRT到項(xiàng)目

目前CoreRT還是alpha版,在myget上有包。

首先添加一個(gè)nuget.config

dotnet new nuget

然后在 <packageSources> 節(jié)點(diǎn)添加如下:

<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-*

替換默認(rèn)services并處理反射

打開(kāi)Startup.cs ,將 services.AddMvc(); 替換成 services.AddMvcCore().AddJsonFormatters();

接著創(chuàng)建一個(gè)rd.xml 配置文件,用于反射處理運(yùn)行時(shí),確保反射程序集編譯到程序中。

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。

打開(kāi)zeroapi.csproj

<PropertyGroup>節(jié)點(diǎn)下將 <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;
 }
 }

程序改造完成,接著最重要的發(fā)布。

還原并發(fā)布

發(fā)布之前,使用dotnet run 確保功能正常運(yùn)行。

發(fā)布請(qǐng)打開(kāi) x64 Native Tools Command Prompt for VS 2017 ,注意一定在此命令行發(fā)布。在開(kāi)始菜單找到 Visual Studio 2017,打開(kāi)就能找到。

發(fā)布命令還是  dotnet publish -r <RID> -c <Configuration>

這里發(fā)布Windows 64位 dotnet publish -r win-x64 -c release,初次的話會(huì)默認(rèn)還原對(duì)應(yīng)的包,需要一些時(shí)間。

發(fā)布完成后,打開(kāi)bin\x64\release\netcoreapp2.0\win-x64\publish 文件夾,里面zeroapi.exe即為最終文件,執(zhí)行即可。

.NET Core如何實(shí)現(xiàn)單文件發(fā)布靜態(tài)編譯AOT CoreRT

可以看到最終大小為21m左右。然后訪問(wèn)http://localhost:5000/ 如圖:

控制臺(tái)Demo

新建一個(gè)控制臺(tái)應(yīng)用,使用AOT發(fā)布。

單純控制臺(tái),沒(méi)有反射的內(nèi)容,無(wú)需要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 發(fā)布

.NET Core如何實(shí)現(xiàn)單文件發(fā)布靜態(tài)編譯AOT CoreRT

最終單個(gè)文件大小不到4m!

看完了這篇文章,相信你對(duì)“.NET Core如何實(shí)現(xiàn)單文件發(fā)布靜態(tài)編譯AOT CoreRT”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI