netcore webapi 服务(win-service)部署

一、托管服务

  1. 安装依赖
    PM> NuGet\Install-Package Microsoft.Extensions.Hosting.WindowsServices -Version 9.0.0
    
  2. 启动端口
    {
     "urls":"http://*:5100"
    }
    
  3. 服务托管
    var builder = WebApplication.CreateBuilder(args);
    
    // win service
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
         builder.Host.UseWindowsService();
    
     var app = builder.Build();
    
  4. 启动脚本 start.bat
    @echo off
    
    :: 发布路径
    set path="E:\Apricot\bin\Release\net8.0\publish"
    
    :: 启动文件
    set app="Apricot.exe"
    
    :: 创建服务
    %SystemRoot%\system32\sc.exe create Apricot.Service  binPath="%path%/%app%"  start=auto displayname="Apricot.Service"
    
    :: 设置描述
    %SystemRoot%\system32\sc.exe description Apricot.Service "Apricot.Service 托管服务。"
    
    :: 启动服务
    echo Apricot.Service starting...
    
    %SystemRoot%\system32\net.exe start Apricot.Service
    
    echo  Apricot.Service started.
    
    pause
    
  5. 停止脚本 stop.bat
    @echo off
    
     echo ============Apricot.Service Stoping ============
     %SystemRoot%\system32\net.exe stop Apricot.Service
     echo ============Apricot.Service Stoped============
    
     echo ============Apricot.Service Deleting============
     %SystemRoot%\system32\sc.exe delete Apricot.Service
     echo ============Apricot.Service Deteted Start============
    
     pause
    

二、nssm 服务安装

  1. 下载 nssm[官网]

  2. 打开 dos管理员身份运行

    1、转到下载目录

  3. 打开 GUI 界面

  4. 配置安装

  5. 安装服务

  6. 启动服务

  7. 查看应用

  8. 帮助命令

posted @ 2023-03-08 17:08  1764564459  阅读(229)  评论(0)    收藏  举报