快捷搜索:
当前位置: 银河手机版app > 银河手机版app > 正文

Core单文件发布静态编译AOT

时间:2019-11-15 05:42来源:银河手机版app
.NET Core单文件发表静态编写翻译AOT CoreRT,将.NETCore应用打包成贰个可实行文件并蕴藏运行时。 支持Windows, MacOS and Linux x64 w/ RyuJIT codegen。 示范项目: 下边来其实体会。 首先保险安装C

.NET Core单文件发表静态编写翻译AOT CoreRT,将.NET Core应用打包成贰个可实行文件并蕴藏运行时。

支持Windows, MacOS and Linux x64 w/ RyuJIT codegen。

示范项目:

下边来其实体会。

首先保险安装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="" />
<add key="nuget.org" value="" 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配置文件如下:

图片 1图片 2

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

View Code

此中假令你要替换来自身的前后相继,校正 <Assembly Name="zeroapi" Dynamic="Required All" /> 中的Name。

打开zeroapi.csproj

在<PropertyGroup>节点下将 <途睿欧dXmlFile>rd.xml</WranglerdXmlFile> 参与。

随着将 <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 <安德拉ID> -c <Configuration>

此地宣布Windows 六15人 dotnet publish -r win-x64 -c release,初次的话会暗中认可还原对应的包,须要有的时辰。

公布实现后,展开binx64releasenetcoreapp2.0win-x64publish 文件夹,里面zeroapi.exe即为最终文件,施行就可以。

图片 3

 

能够看到最终大小为21m左右。然后访谈 如图:

图片 4

 

控制台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 发布

图片 5

谈起底单个文件大小不到4m!

眼前该技术或许开始的风流浪漫段时代版本,期望微软最后带到正式版。

GitHub:

编辑:银河手机版app 本文来源:Core单文件发布静态编译AOT

关键词: