本文共 1667 字,大约阅读时间需要 5 分钟。
在将“.NET 跨平台之旅”示例站点从 ASP.NET 5 RC1 升级至 ASP.NET Core 1.0 之后,我们不禁想要体验一个激动人心的特性:将 .NET 程序编译为本地(native)机器码。对于我们的示例站点,这意味着将 ASP.NET Core 应用程序编译为本地机器码。
dotnet build --native
Project AboutUs (.NETStandardApp,Version=v1.3) will be compiled because some of its inputs were newer than its oldest output.Compiling AboutUs for .NETStandardApp,Version=v1.3Compilation succeeded.0 Warning(s)0 Error(s)Time elapsed 00:00:02.7095915Input Assembly: /data/AboutUs/bin/Debug/netstandardapp1.3/AboutUs.dll
在 bin/Debug/netstandardapp1.3/ 文件夹中,你可以看到以下内容:
ubuntu.14.04-x64 文件夹中的输出即为本地编译的结果,其中 AboutUs 是可执行文件。about.cnblogs.com 文件夹,并将 ubuntu.14.04-x64 文件夹中的所有文件复制到这个文件夹中。config.json 文件复制到 about.cnblogs.com 文件夹中。示例站点需要进行数据库操作(数据库使用 SQL Server),config.json 中存储着数据库连接字符串。在 about.cnblogs.com 文件夹中运行以下命令:
./AboutUs
dbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[3] Hosting startingdbug: Microsoft.AspNetCore.Hosting.Internal.WebHost[4] Hosting startedHosting environment: ProductionApplication base path: /data/websites/about.cnblogs.comNow listening on: http://*:8001Application started. Press Ctrl+C to shut down.
太棒了!站点已经成功运行!在浏览器中访问站点,你可以看到一个正常运行的页面。现在你看到的,就是在 Linux Ubuntu 服务器上以本地方式运行的 .NET 应用程序。
在编译过程中,我们曾遇到过 ubuntu.14.04-x64 文件夹中无输出的情况。为了解决这个问题,可以改用以下命令:
dotnet build --native --no-incremental
后来我们遇到了502错误,原因是没有以后台服务的方式运行命令,SSH 会话一旦断开,进程就结束了。为了解决这个问题,我们改为使用以下命令运行:
screen -d -m -s "AboutUs" /data/websites/about.cnblogs.com/AboutUs
这就是从 ASP.NET 5 RC1 升级到 ASP.NET Core 1.0 之后的完整编译与运行过程。通过本地编译,我们不仅体验了跨平台开发的便利,还为未来的性能优化和资源使用打下了坚实的基础。
转载地址:http://saekz.baihongyu.com/