添加 Windows 服务支持和 EF 工具配置

在 `FastGateway.Service.csproj` 文件中,添加了 `Microsoft.Extensions.Hosting.WindowsServices` 包的引用,并添加了 `install-window.bat` 文件,设置为始终复制到输出目录。
在 `Program.cs` 文件中,添加了 `System.Runtime.InteropServices` 的引用,并在 `Main` 方法中设置了当前目录为应用程序的基础目录,根据操作系统判断是否为 Windows,如果是则添加 Windows 服务配置。
新增了 `install-window.bat` 文件,用于在 Windows 上安装和配置 `FastGateway` 服务。
新增了 `dotnet-tools.json` 文件,添加了 `dotnet-ef` 工具的配置。
This commit is contained in:
token
2024-10-24 01:47:18 +08:00
parent e36a3660ec
commit 451d142f7e
4 changed files with 64 additions and 1 deletions
@@ -0,0 +1,13 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "8.0.10",
"commands": [
"dotnet-ef"
],
"rollForward": false
}
}
}
@@ -25,6 +25,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.0-rc.2.24473.5" />
<PackageReference Include="Serilog" Version="4.0.1-dev-02205" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
@@ -39,6 +40,9 @@
<None Update="gateway.pfx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="install-window.bat">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="ip2region.xdb">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
+17 -1
View File
@@ -1,3 +1,4 @@
using System.Runtime.InteropServices;
using System.Text;
using FastGateway.Service.BackgroundTask;
using FastGateway.Service.DataAccess;
@@ -18,7 +19,13 @@ public static class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
ContentRootPath = AppContext.BaseDirectory,
Args = args
});
var logger = new LoggerConfiguration()
.WriteTo.Console()
@@ -29,6 +36,15 @@ public static class Program
builder.Host.UseSerilog(logger);
builder.Services.AddHttpClient();
// 判断是否window,
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
builder.Services.AddWindowsService(option =>
{
option.ServiceName = "FastGateway";
});
}
var jwtOptions = builder.Configuration.GetSection(JwtOptions.Name).Get<JwtOptions>();
builder.Services
@@ -0,0 +1,30 @@
@echo off
REM 设置控制台代码页为 UTF-8
chcp 65001 >nul
REM 检查是否以管理员身份运行
openfiles >nul 2>&1
if %errorlevel% neq 0 (
echo 请以管理员身份运行此脚本。
pause
exit /b
)
REM 设置服务名称和显示名称
set SERVICE_NAME=FastGateway
set DISPLAY_NAME=FastGateway管理网关,用于管理服务和代理服务。
REM 设置服务的可执行文件路径
set SERVICE_EXE=%~dp0FastGateway.Service.exe
REM 创建服务
sc create %SERVICE_NAME% binPath= "%SERVICE_EXE%" DisplayName= "%DISPLAY_NAME%"
REM 设置服务为自动启动
sc config %SERVICE_NAME% start= auto
REM 启动服务
sc start %SERVICE_NAME%
echo 服务 %DISPLAY_NAME% 已创建并设置为开机自启动。
pause