构建.NET Core Web API为Windows服务安装包

要将.NET Core Web API制作成Windows服务安装包,可以按照以下步骤进行操作:

  1. 创建一个新的.NET Core Web API项目或使用现有的项目。

  2. 在项目中添加对Microsoft.Extensions.Hosting.WindowsServices包的引用。可以通过NuGet包管理器或在.csproj文件中手动添加引用。

  3. 在Program.cs文件中,修改CreateHostBuilder方法,以便将Web API应用程序作为Windows服务运行。示例如下:

csharp 复制代码
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;

public class Program
{
    public static void Main(string[] args)
    {
        var isService = !(Debugger.IsAttached || args.Contains("--console"));

        var builder = CreateHostBuilder(args.Where(arg => arg != "--console").ToArray());

        if (isService)
        {
            builder.UseWindowsService();
        }

        var host = builder.Build();

        if (isService)
        {
            host.RunAsService();
        }
        else
        {
            host.Run();
        }
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
  1. 在项目的根目录中创建一个service文件夹,并在其中添加一个serviceinstaller.ps1文件。该文件用于安装和卸载Windows服务。示例如下:
powershell 复制代码
param (
    [switch]$uninstall
)

$serviceName = "YourServiceName"
$serviceDisplayName = "Your Service Display Name"
$serviceDescription = "Your Service Description"
$serviceExePath = "$PSScriptRoot\..\YourServiceProjectName.dll"

if ($uninstall)
{
    Write-Host "Uninstalling $serviceName..."
    sc.exe delete $serviceName
    Write-Host "Uninstall completed."
}
else
{
    Write-Host "Installing $serviceName..."
    sc.exe create $serviceName binPath= $serviceExePath DisplayName= $serviceDisplayName start= auto
    sc.exe description $serviceName $serviceDescription
    Write-Host "Install completed."
}
  1. 在项目的根目录中创建一个publish.bat文件,用于发布项目并生成安装包。示例如下:
batch 复制代码
@echo off

dotnet publish -c Release -o .\publish

powershell -ExecutionPolicy Bypass -File .\service\serviceinstaller.ps1
  1. 打开命令提示符或PowerShell,导航到项目的根目录,并运行publish.bat文件。这将发布项目并生成安装包。

  2. 在生成的publish文件夹中,找到发布的Web API应用程序和安装脚本。

  3. 将整个publish文件夹复制到目标服务器上,并运行serviceinstaller.ps1脚本以安装Windows服务。可以使用以下命令:

powershell 复制代码
powershell -ExecutionPolicy Bypass -File .\service\serviceinstaller.ps1

安装完成后,.NET Core Web API将作为Windows服务在目标服务器上运行。

请注意,以上步骤仅适用于将.NET Core Web API作为Windows服务安装。如果需要更高级的功能,例如服务启动类型、日志记录等,可能需要进一步的自定义和配置。

相关推荐
雪落漂泊21 小时前
Liunux进程(七):程序控制篇(中)
linux·运维·windows
承渊政道2 天前
Fooocus本地AI绘图教程:Windows部署、低门槛生图与远程访问
人工智能·windows·cpolar·fooocus·生图
陈工大模型2 天前
收到乱码压缩包之后,我重新测了 7-Zip、Bandizip 和win解压缩
windows·电脑·软件工程·软件需求·7-zip
谙弆悕博士2 天前
在文件夹右键直接打开 Kali WSL:一条 `wsl --cd` 注册表脚本
windows·脚本·wsl·右击·ksli
陈工大模型2 天前
解压工具的三个技术分水岭:编码、分卷、容器格式
人工智能·windows·安全·解压工具·免费国内办公软件
Zucker n3 天前
文献发现、课题组织与证据可追溯的论文阅读。
论文阅读·windows·ubuntu
flyingonpine4 天前
PowerShell脚本数字签名实战:自签名证书、代码签名与信任链验证
windows·信任链
goehou4 天前
让 AI 替我操作这台电脑,先把手能伸多远定下来
windows·自动化·权限管理·ai agent
公子小六4 天前
基于.NET的Windows窗体编程之WinForms数据持久化(1)
windows·microsoft·c#·.net·winforms
2601_967987374 天前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装