From eb3ef70a787b50613ea79583bd1c11de7d94b008 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=80=81=E4=B9=9D?= <366193849@qq.com>
Date: Sat, 25 Sep 2021 11:56:36 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8Run=E6=89=A9=E5=B1=95?=
=?UTF-8?q?=E5=90=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
FastGithub/Program.cs | 2 +-
FastGithub/WindowServiceExtensions.cs | 76 ++++++++++++++++-----------
2 files changed, 46 insertions(+), 32 deletions(-)
diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs
index 72e1bf4..fd8dc61 100644
--- a/FastGithub/Program.cs
+++ b/FastGithub/Program.cs
@@ -15,7 +15,7 @@ namespace FastGithub
///
public static void Main(string[] args)
{
- CreateHostBuilder(args).Build().RunWithWindowsServiceControl();
+ CreateHostBuilder(args).Build().Run();
}
///
diff --git a/FastGithub/WindowServiceExtensions.cs b/FastGithub/WindowServiceExtensions.cs
index 9de3750..0981c95 100644
--- a/FastGithub/WindowServiceExtensions.cs
+++ b/FastGithub/WindowServiceExtensions.cs
@@ -5,6 +5,7 @@ using PInvoke;
using System;
using System.IO;
using System.Linq;
+using System.Runtime.Versioning;
using static PInvoke.AdvApi32;
namespace FastGithub
@@ -40,45 +41,56 @@ namespace FastGithub
}
///
- /// 以支持windows服务控制的方式运行
+ /// 运行主机
///
///
- public static void RunWithWindowsServiceControl(this IHost host)
+ public static void Run(this IHost host)
+ {
+ if (OperatingSystem.IsWindows() && TryGetCommand(out var cmd))
+ {
+ try
+ {
+ UseCommand(cmd);
+ }
+ catch (Exception ex)
+ {
+ var loggerFactory = host.Services.GetRequiredService();
+ loggerFactory.CreateLogger(nameof(FastGithub)).LogError(ex.Message);
+ }
+ }
+ else
+ {
+ HostingAbstractionsHostExtensions.Run(host);
+ }
+ }
+
+ ///
+ /// 获取控制指令
+ ///
+ ///
+ ///
+ private static bool TryGetCommand(out Command cmd)
{
var args = Environment.GetCommandLineArgs();
- if (OperatingSystem.IsWindows() == false ||
- Enum.TryParse(args.Skip(1).FirstOrDefault(), true, out var cmd) == false)
- {
- host.Run();
- return;
- }
+ return Enum.TryParse(args.Skip(1).FirstOrDefault(), true, out cmd);
+ }
- try
+ ///
+ /// 应用控制指令
+ ///
+ ///
+ [SupportedOSPlatform("windows")]
+ private static void UseCommand(Command cmd)
+ {
+ var binaryPath = Environment.GetCommandLineArgs().First();
+ var serviceName = Path.GetFileNameWithoutExtension(binaryPath);
+ if (cmd == Command.Start)
{
- var binaryPath = args.First();
- var serviceName = Path.GetFileNameWithoutExtension(binaryPath);
-
- if (cmd == Command.Start)
- {
- InstallAndStartService(serviceName, binaryPath);
- }
- else if (cmd == Command.Stop)
- {
- StopAndDeleteService(serviceName);
- }
+ InstallAndStartService(serviceName, binaryPath);
}
- catch (Exception ex)
+ else if (cmd == Command.Stop)
{
- var loggerFactory = host.Services.GetService();
- if (loggerFactory != null)
- {
- var logger = loggerFactory.CreateLogger(nameof(WindowServiceExtensions));
- logger.LogError(ex.Message);
- }
- else
- {
- Console.WriteLine(ex.Message);
- }
+ StopAndDeleteService(serviceName);
}
}
@@ -88,6 +100,7 @@ namespace FastGithub
///
///
/// exception >
+ [SupportedOSPlatform("windows")]
private static void InstallAndStartService(string serviceName, string binaryPath)
{
using var hSCManager = OpenSCManager(null, null, ServiceManagerAccess.SC_MANAGER_ALL_ACCESS);
@@ -131,6 +144,7 @@ namespace FastGithub
///
///
/// exception >
+ [SupportedOSPlatform("windows")]
private static void StopAndDeleteService(string serviceName)
{
using var hSCManager = OpenSCManager(null, null, ServiceManagerAccess.SC_MANAGER_ALL_ACCESS);