博客
关于我
.NET跨平台之旅:在Linux上以本地机器码(native)运行ASP.NET Core站点
阅读量:414 次
发布时间:2019-03-06

本文共 1667 字,大约阅读时间需要 5 分钟。

.NET 跨平台之旅:从 ASP.NET 5 到 ASP.NET Core 的升级历程

在将“.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/

    你可能感兴趣的文章
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
    查看>>
    npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install CERT_HAS_EXPIRED解决方法
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>