使用Run扩展名

This commit is contained in:
老九 2021-09-25 11:56:36 +08:00
parent a16844c0ea
commit eb3ef70a78
2 changed files with 46 additions and 32 deletions

View File

@ -15,7 +15,7 @@ namespace FastGithub
/// <param name="args"></param> /// <param name="args"></param>
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateHostBuilder(args).Build().RunWithWindowsServiceControl(); CreateHostBuilder(args).Build().Run();
} }
/// <summary> /// <summary>

View File

@ -5,6 +5,7 @@ using PInvoke;
using System; using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Versioning;
using static PInvoke.AdvApi32; using static PInvoke.AdvApi32;
namespace FastGithub namespace FastGithub
@ -40,24 +41,49 @@ namespace FastGithub
} }
/// <summary> /// <summary>
/// 以支持windows服务控制的方式运行 /// 运行主机
/// </summary> /// </summary>
/// <param name="host"></param> /// <param name="host"></param>
public static void RunWithWindowsServiceControl(this IHost host) public static void Run(this IHost host)
{ {
var args = Environment.GetCommandLineArgs(); if (OperatingSystem.IsWindows() && TryGetCommand(out var cmd))
if (OperatingSystem.IsWindows() == false ||
Enum.TryParse<Command>(args.Skip(1).FirstOrDefault(), true, out var cmd) == false)
{ {
host.Run();
return;
}
try try
{ {
var binaryPath = args.First(); UseCommand(cmd);
var serviceName = Path.GetFileNameWithoutExtension(binaryPath); }
catch (Exception ex)
{
var loggerFactory = host.Services.GetRequiredService<ILoggerFactory>();
loggerFactory.CreateLogger(nameof(FastGithub)).LogError(ex.Message);
}
}
else
{
HostingAbstractionsHostExtensions.Run(host);
}
}
/// <summary>
/// 获取控制指令
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
private static bool TryGetCommand(out Command cmd)
{
var args = Environment.GetCommandLineArgs();
return Enum.TryParse(args.Skip(1).FirstOrDefault(), true, out cmd);
}
/// <summary>
/// 应用控制指令
/// </summary>
/// <param name="cmd"></param>
[SupportedOSPlatform("windows")]
private static void UseCommand(Command cmd)
{
var binaryPath = Environment.GetCommandLineArgs().First();
var serviceName = Path.GetFileNameWithoutExtension(binaryPath);
if (cmd == Command.Start) if (cmd == Command.Start)
{ {
InstallAndStartService(serviceName, binaryPath); InstallAndStartService(serviceName, binaryPath);
@ -67,20 +93,6 @@ namespace FastGithub
StopAndDeleteService(serviceName); StopAndDeleteService(serviceName);
} }
} }
catch (Exception ex)
{
var loggerFactory = host.Services.GetService<ILoggerFactory>();
if (loggerFactory != null)
{
var logger = loggerFactory.CreateLogger(nameof(WindowServiceExtensions));
logger.LogError(ex.Message);
}
else
{
Console.WriteLine(ex.Message);
}
}
}
/// <summary> /// <summary>
/// 安装并启动服务 /// 安装并启动服务
@ -88,6 +100,7 @@ namespace FastGithub
/// <param name="serviceName"></param> /// <param name="serviceName"></param>
/// <param name="binaryPath"></param> /// <param name="binaryPath"></param>
/// <exception cref = "Win32Exception" ></ exception > /// <exception cref = "Win32Exception" ></ exception >
[SupportedOSPlatform("windows")]
private static void InstallAndStartService(string serviceName, string binaryPath) private static void InstallAndStartService(string serviceName, string binaryPath)
{ {
using var hSCManager = OpenSCManager(null, null, ServiceManagerAccess.SC_MANAGER_ALL_ACCESS); using var hSCManager = OpenSCManager(null, null, ServiceManagerAccess.SC_MANAGER_ALL_ACCESS);
@ -131,6 +144,7 @@ namespace FastGithub
/// </summary> /// </summary>
/// <param name="serviceName"></param> /// <param name="serviceName"></param>
/// <exception cref = "Win32Exception" ></ exception > /// <exception cref = "Win32Exception" ></ exception >
[SupportedOSPlatform("windows")]
private static void StopAndDeleteService(string serviceName) private static void StopAndDeleteService(string serviceName)
{ {
using var hSCManager = OpenSCManager(null, null, ServiceManagerAccess.SC_MANAGER_ALL_ACCESS); using var hSCManager = OpenSCManager(null, null, ServiceManagerAccess.SC_MANAGER_ALL_ACCESS);