构建.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服务安装。如果需要更高级的功能,例如服务启动类型、日志记录等,可能需要进一步的自定义和配置。

相关推荐
集成显卡17 小时前
windows 下使用 bat 批处理运行 Chrome 无头模式刷一波访问量
windows·程序员
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
研华嵌入式3 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
带娃的IT创业者4 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp
csdn_aspnet4 天前
Windows Node.js 安装及环境配置详细教程
windows·node.js
摇滚侠4 天前
java语言中,list<String>转成字符串,逗号分割;List<Integer>转字符串,逗号分割
java·windows·list
Source.Liu4 天前
【Pywinauto库】12.2 pywinauto.element_info 后端内部实施模块
windows·python·自动化
Source.Liu4 天前
【Pywinauto库】12.1 pywinauto.backend 后端内部实施模块
开发语言·windows·python·自动化
私人珍藏库4 天前
[Windows] FileOptimizer v17.1.0_一款文件批量压缩工具
windows·批量压缩
掘根4 天前
【CMake】List
windows·microsoft·list