From 0bf8998d26ac0401a4b42bdec0ab13b145961d70 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 14:08:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E4=BE=8B=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FastGithub/Program.cs | 3 +-- FastGithub/WindowServiceExtensions.cs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/FastGithub/Program.cs b/FastGithub/Program.cs index fd8dc61..d798dfb 100644 --- a/FastGithub/Program.cs +++ b/FastGithub/Program.cs @@ -27,8 +27,7 @@ namespace FastGithub { return Host .CreateDefaultBuilder(args) - .UseWindowsService() - .UseBinaryPathContentRoot() + .UseWindowsService() .UseDefaultServiceProvider(c => { c.ValidateOnBuild = false; diff --git a/FastGithub/WindowServiceExtensions.cs b/FastGithub/WindowServiceExtensions.cs index 0981c95..02c640f 100644 --- a/FastGithub/WindowServiceExtensions.cs +++ b/FastGithub/WindowServiceExtensions.cs @@ -6,6 +6,7 @@ using System; using System.IO; using System.Linq; using System.Runtime.Versioning; +using System.Threading; using static PInvoke.AdvApi32; namespace FastGithub @@ -25,11 +26,11 @@ namespace FastGithub } /// - /// 使用应用程序文件所在目录作为ContentRoot + /// 使用windows服务 /// - /// + /// /// - public static IHostBuilder UseBinaryPathContentRoot(this IHostBuilder hostBuilder) + public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder) { var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First()); if (contentRoot != null) @@ -37,14 +38,15 @@ namespace FastGithub Environment.CurrentDirectory = contentRoot; hostBuilder.UseContentRoot(contentRoot); } - return hostBuilder; + return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder); } /// /// 运行主机 /// /// - public static void Run(this IHost host) + /// + public static void Run(this IHost host, bool singleton = true) { if (OperatingSystem.IsWindows() && TryGetCommand(out var cmd)) { @@ -60,7 +62,11 @@ namespace FastGithub } else { - HostingAbstractionsHostExtensions.Run(host); + using var mutex = new Mutex(true, "Global\\FastGithub", out var firstInstance); + if (singleton == false || firstInstance) + { + HostingAbstractionsHostExtensions.Run(host); + } } }