单例运行

This commit is contained in:
老九 2021-09-25 14:08:49 +08:00
parent eb3ef70a78
commit 0bf8998d26
2 changed files with 13 additions and 8 deletions

View File

@ -28,7 +28,6 @@ namespace FastGithub
return Host return Host
.CreateDefaultBuilder(args) .CreateDefaultBuilder(args)
.UseWindowsService() .UseWindowsService()
.UseBinaryPathContentRoot()
.UseDefaultServiceProvider(c => .UseDefaultServiceProvider(c =>
{ {
c.ValidateOnBuild = false; c.ValidateOnBuild = false;

View File

@ -6,6 +6,7 @@ using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Versioning; using System.Runtime.Versioning;
using System.Threading;
using static PInvoke.AdvApi32; using static PInvoke.AdvApi32;
namespace FastGithub namespace FastGithub
@ -25,11 +26,11 @@ namespace FastGithub
} }
/// <summary> /// <summary>
/// 使用应用程序文件所在目录作为ContentRoot /// 使用windows服务
/// </summary> /// </summary>
/// <param name="hostBuilder"></param> /// <param name="hostBuilder"></param>
/// <returns></returns> /// <returns></returns>
public static IHostBuilder UseBinaryPathContentRoot(this IHostBuilder hostBuilder) public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder)
{ {
var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First()); var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First());
if (contentRoot != null) if (contentRoot != null)
@ -37,14 +38,15 @@ namespace FastGithub
Environment.CurrentDirectory = contentRoot; Environment.CurrentDirectory = contentRoot;
hostBuilder.UseContentRoot(contentRoot); hostBuilder.UseContentRoot(contentRoot);
} }
return hostBuilder; return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder);
} }
/// <summary> /// <summary>
/// 运行主机 /// 运行主机
/// </summary> /// </summary>
/// <param name="host"></param> /// <param name="host"></param>
public static void Run(this IHost host) /// <param name="singleton"></param>
public static void Run(this IHost host, bool singleton = true)
{ {
if (OperatingSystem.IsWindows() && TryGetCommand(out var cmd)) if (OperatingSystem.IsWindows() && TryGetCommand(out var cmd))
{ {
@ -60,7 +62,11 @@ namespace FastGithub
} }
else else
{ {
HostingAbstractionsHostExtensions.Run(host); using var mutex = new Mutex(true, "Global\\FastGithub", out var firstInstance);
if (singleton == false || firstInstance)
{
HostingAbstractionsHostExtensions.Run(host);
}
} }
} }