以及.NET Core 2.0 以上版本SDK,以及.NET Core 2.0 以上版本SDK365体育网投

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

365体育网投 1

末尾单个文件大小不到4m!

此时此刻该技术或者早期版本,期待微软最终带到正式版。

GitHub:https://github.com/dotnet/corert

.NET Core单文件披露静态编译AOT CoreRT,将.NET
Core应用打包成一个可执行文件并包含运行时。

下边来实在体验。

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

演示项目:

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

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

示范项目:

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

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

365体育网投 2

终极单个文件大小不到4m!

脚下该技术依然早期版本,期待微软最后带到正式版。

GitHub:https://github.com/dotnet/corert

新建WebAPI应用

打开命令指示符,输入如下:

dotnet new webapi -o zeroapi

cd zeroapi

新建WebAPI应用。

新建WebAPI应用

开辟命令指示符,输入如下:

dotnet new webapi -o zeroapi

cd zeroapi

新建WebAPI应用。

复原并发表

揭橥以前,使用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即为最终文件,执行即可。

365体育网投 3

 

可以观察最后大小为21m左右。然后访问http://localhost:5000/ 如图:

365体育网投 4

 

下边来实在体验。

.NET Core单文件披露静态编译AOT CoreRT,将.NET
Core应用打包成一个可执行文件并带有运行时。

https://github.com/dotnet/corert/tree/master/samples/WebApi

第一保证安装C++编译环境,以及.NET Core 2.0 以上版本SDK。

率先保证安装C++编译环境,以及.NET Core 2.0 以上版本SDK。

轮换默认services并处理反射

打开Startup.cs ,将 services.AddMvc();
替换成 services.AddMvcCore().AddJsonFormatters();

进而创造一个rd.xml
配置文件,用于反射处理运行时,确保反射程序集编译到程序中。

rd.xml配置文件如下:

365体育网投 5365体育网投 6

<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>节点下将 <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;
        }
    }

先后改造形成,接着最重大的颁发。

轮换默认services并拍卖反射

打开Startup.cs ,将 services.AddMvc();
替换成 services.AddMvcCore().AddJsonFormatters();

随着创设一个rd.xml
配置文件,用于反射处理运行时,确保反射程序集编译到程序中。

rd.xml配置文件如下:

365体育网投 7365体育网投 8

<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>节点下将 <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;
        }
    }

先后改造形成,接着最着重的昭示。

https://github.com/dotnet/corert/tree/master/samples/WebApi

復苏并发表

揭橥从前,使用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即为最终文件,执行即可。

365体育网投 9

 

可以见到最终大小为21m左右。然后访问http://localhost:5000/ 如图:

365体育网投 10

 

相关文章