返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Asp-Net-Core学习笔记

Asp-Net-Core Study Notes: 5. Build and Deploy

Since I started learning Asp.Net Core last time, I've just been playing around with it and haven't had much chance to use it in a real project. Recently the company had a new small project, so I tried Asp.Net Core to get a taste of something new, and the result was very OK. After getting familiar with it, development efficiency felt basically no different from Django. Then let's get to the point. This article briefly talks about building and deploying Asp.Net Core applications. Build Building uses the build command, but generally we use IDEs like VS or Rider, so we probably rarely use this command: dotn

曦远 (DealiAxy)约 16 分钟
目录5 个章节

Since I started learning Asp.Net Core last time, I've just been playing around with it and haven't had much chance to use it in a real project. Recently the company had a new small project, so I tried Asp.Net Core to get a taste of something new, and the result was very OK. After getting familiar with it, development efficiency felt basically no different from Django.

Then let's get to the point. This article briefly talks about building and deploying Asp.Net Core applications.

Build

Building uses the build command, but generally we use IDEs like VS or Rider, so we probably rarely use this command:

dotnet build

Publish

This article will focus on the publishing part. As the name suggests, publishing means compiling the project into an executable file, so we can conveniently take it to other machines or deploy it to a server to run. For Java, that means packaging into a jar that depends on the JavaRuntime, or a war package that can run on Tomcat; for Go, it generates an executable file—just run it and you're done; for Python, sorry, there doesn't seem to be a great solution—you can package it as a wheel, or use pyinstaller to pack an exe, but for deploying web apps, people generally still use Docker.

As for our Asp.Net Core applications, just like Go, it's very convenient—you can pack it into a single executable file. One word: awesome!

Reference: https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-publish

The simplest publish command is:

dotnet publish

After using this command, a whole bunch of files will be generated in the publish directory, including dependencies, the app's core dll package, and the executable for the corresponding platform. Please note that this pile of generated files depends on the .NetCore Runtime on the machine to run.

For convenience, we definitely don't want to still need to install the .NetCore Runtime on the server when deploying, so see below.

To build an executable that does not depend on the DotNet Sdk, you need to specify the target platform:

dotnet publish -r <RID>

For RID, see: https://docs.microsoft.com/zh-cn/dotnet/core/rid-catalog

Here are a few commonly used ones:

  • Windows
    • win-x86
    • win-x64
    • win-arm
    • win-arm64
  • Linux
    • linux-x64
    • linux-arm
    • linux-arm64

For example, if I want to build an executable that can be used on a Linux server, use the following command:

dotnet publish -r linux-x64

The generated files look like this:

As you can see, this huge pile has more than three hundred folders and files. Putting this on a server is really a hassle—it takes up space and looks ugly. Let's also look at the size:

Over a hundred MB, that's way too much space. Uploading to the server would take forever. But don't worry, Microsoft's black magic is coming next~

Add these two options

dotnet publish -r linux-x64 -c Release -p:PublishSingleFile=true -p:PublishTrimmed=true

Run it, whoosh! Let's look at the generated files again—there's only one executable, a few config files, and the web static files directory! (Because I used the Debug build, there are pdb debug files.) Much cleaner than before!

Let's look at the file size again:

Dozens of MB smaller than before! That's much better now!

Next I'll introduce those extra two parameters. I feel like I don't even need to introduce them—everyone can guess~

  • -c Release means generate with the Release configuration
  • -p:PublishSingleFile=true means generate a single executable file, like Go
  • -p:PublishTrimmed=true means trim the size of the executable, just as you saw, the size shrank by dozens of MB!

This way you can just drop this executable onto the server and run it. But once SSH disconnects, it's gone again~

So next I'll introduce using supervisor to monitor the web app's running.

Deploy

We choose supervisor, this simple tool, to manage and monitor the web app's running. It's written in Python, simple and easy to use.

First you need to install it, you can use pip:

pip install supervisor

Then create our config under the /etc/supervisord.d/ directory. Here's a template for reference.

[program:swatow_affairs]
# 脚本目录
directory = /path/dotnet/swatow_affairs
# 脚本执行命令
command = /path/to/swatow_affairs/SwatowAffairsBackend --urls "http://*:15001;"

# supervisor启动的时候是否随着同时启动,默认True
autostart = true
# 当程序exit的时候,这个program不会自动重启,默认unexpected,设置子进程挂掉后自动重启的情况,
# 有三个选项,false,unexpected和true。
# 如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有当进程的退出码不在下面的exitcodes里面定义的
autorestart = true
# 这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs = 1

# 脚本运行的用户身份 
user = root

# 日志输出 
stderr_logfile = /path/to/swatow_affairs/log/stderr.log
stdout_logfile = /path/to/swatow_affairs/log/stdout.log
# 把stderr重定向到stdout,默认 false
redirect_stderr = true
# stdout日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20
# stdout日志文件备份数
stdout_logfile_backups = 20

After configuring this file, you can start supervisor. The command is as follows:

systemctl start supervisord.service

You can also use:

service supervisord start

Commands differ across Linux distros; you can look up the docs yourself.

After starting supervisor, our config file will be loaded automatically, and then our web app will start along with it.

For more configuration, see supervisor's official docs: https://github.com/Supervisor/supervisor

References

Welcome to Discuss

I've put together a series of technical articles and materials. Reply linux, flutter, c#, netcore, android, kotlin, java, python, etc. in the backend of the WeChat official account "Programming Lab" to get related technical articles and materials. If you have any questions, you can also leave a message in the official account backend~

_EOF_

Asp-Net-Core Study Notes: 5. Build and Deploy

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Asp-Net-Core学习笔记:部署,早知道,还是docker,以及一点碎碎念

前言 AspNetCore技术栈在我们团队里的使用也有一段时间了,之前的部署方式一直是本地编译之后上传可执行文件到服务器,使用supervisor来管理进程这种很原始的方式。 参考之前的文章: 对于小项目来说尚可,够用,但是存在几个问题: 每次更新花费的时间太长了,无论是Framework-Dependent还是Self-Contained,都要上传很大的文件~ 更新的时候需要在superviso

2022-02-16 · Asp-Net-Core学习笔记 · 约 1 分钟

Asp-Net-Core学习笔记:身份认证入门

前言 过年前我又来更新了~ 我就说了最近不是在偷懒吧,其实这段时间还是有积累一些东西的,不过还没去整理…… 所以只能发以前没写完的一些笔记出来 就当做是温习一下啦 PS:之前说的红包封面我还没搞,得抓紧时间了 最近在准备搞一个我之前做的开源项目代码合集来做一期分享 两种常见的认证方式 先来看看两种常见的认证方式:基于token的认证和传统的session认证的区别。 session认证 我们知道,

2022-02-16 · Asp-Net-Core学习笔记 · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…