惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
T
Threatpost
Latest news
Latest news
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Help Net Security
Help Net Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
AI
AI
Simon Willison's Weblog
Simon Willison's Weblog
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
T
Threat Research - Cisco Blogs
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Exploit Database - CXSecurity.com
NISL@THU
NISL@THU
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Securelist
小众软件
小众软件
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
Cisco Talos Blog
Cisco Talos Blog
云风的 BLOG
云风的 BLOG
AWS News Blog
AWS News Blog
GbyAI
GbyAI
N
News and Events Feed by Topic
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
美团技术团队
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
博客园 - 聂微东
V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
SecWiki News
SecWiki News
S
Secure Thoughts
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
腾讯CDC
H
Heimdal Security Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed

博客园 - 石曼迪

Visual Studio 2026 开发 MAUI app 记录 Windows下搭建rustdesk远程服务 nginx 二级域名配置 .NET CORE 相关配置 frps-frpc配置 使用SuperSocket开发联网斗地主(四):出牌 使用SuperSocket开发联网斗地主(三):抢地主 使用SuperSocket开发联网斗地主(二):发牌 使用SuperSocket开发联网斗地主(一):联网 SuperSocket服务端与客户端搭建 EXCEL脚本收藏 小程序开发记录 Python练习 Python基础总结 在centos7.6上部署.netcore 3.0 web程序 最简单的centos上安装Nginx办法 七牛网数据备份 solr搜索分词优化 Ionic异常及解决 狗日的腾讯
.net core web api 发布填坑
石曼迪 · 2026-06-26 · via 博客园 - 石曼迪

部署为 Windows 服务(最直接)

如果你的服务器是 Windows 系统,这是最推荐的方案。应用会像 SQL Server 一样在后台运行,开机自启、不会因误关控制台而中断

有种常用的实现方法:

  • 使用 Microsoft.Extensions.Hosting.WindowsServices 库(官方推荐)
    这是最优雅、最“.NET Core”的方式。只需在代码中做微小改动,就能让应用以Windows服务的方式运行

    1. 在项目的 Program.cs 中,为宿主构建器添加 .UseWindowsService() 扩展方法

    2. (关键步骤) 为避免服务运行时找不到文件,需要手动设置内容根目录

      var options_args = new WebApplicationOptions
      {
          Args = args,
          ContentRootPath = WindowsServiceHelpers.IsWindowsService() 
                             ? AppContext.BaseDirectory 
                             : default
      };
      var builder = WebApplication.CreateBuilder(options_args);
      builder.Host.UseWindowsService(); 
      
    3. 发布应用后,使用管理员权限打开命令行,通过 sc create 命令创建服务

      # 1. 停止并删除旧服务
      sc stop 你的服务名
      sc delete 你的服务名
      
      # 2. 用正确的路径重新创建服务
      sc create 你的服务名 binPath= "D:\MyApp\publish\MyApp.exe" start= auto

      注释要去掉

    4. 然后发现部署成功了,但只能localhost访问,在项目根目录的 appsettings.json 文件中添加以下配置
      {
        "Kestrel": {
          "Endpoints": {
            "Http": {
              "Url": "http://*:5000"
            }
          }
        }
      }

      当然,防火墙要放开端口