您的位置:金沙手机版下载 > 金沙手机版下载 > Core单文件发布静态编译AOT

Core单文件发布静态编译AOT

2019-10-10 16:48

.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>节点下将 <PAJEROdXmlFile>rd.xml</LANDdXmlFile> 加入。

随后将 <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 <CRUISERID> -c <Configuration>

这里发布Windows 陆拾三个人 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:

本文由金沙手机版下载发布于金沙手机版下载,转载请注明出处:Core单文件发布静态编译AOT

关键词: