互斥启动

This commit is contained in:
xljiulang 2022-09-19 19:33:28 +08:00
parent 51170e68b1
commit ef4ebf738a
2 changed files with 8 additions and 9 deletions

View File

@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using System; using System;
using System.IO; using System.IO;
using System.Linq;
namespace FastGithub namespace FastGithub
{ {
@ -15,8 +14,7 @@ namespace FastGithub
public static void Main(string[] args) public static void Main(string[] args)
{ {
ConsoleUtil.DisableQuickEdit(); ConsoleUtil.DisableQuickEdit();
var contentRoot = Path.GetDirectoryName(Environment.ProcessPath);
var contentRoot = Path.GetDirectoryName(Environment.GetCommandLineArgs().First());
if (string.IsNullOrEmpty(contentRoot) == false) if (string.IsNullOrEmpty(contentRoot) == false)
{ {
Environment.CurrentDirectory = contentRoot; Environment.CurrentDirectory = contentRoot;
@ -26,7 +24,7 @@ namespace FastGithub
Args = args, Args = args,
ContentRootPath = contentRoot ContentRootPath = contentRoot
}; };
CreateWebApplication(options).Run(); CreateWebApplication(options).Run(singleton: true);
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using FastGithub.DomainResolve; using FastGithub.DomainResolve;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -38,24 +39,24 @@ namespace FastGithub
/// <param name="hostBuilder"></param> /// <param name="hostBuilder"></param>
/// <returns></returns> /// <returns></returns>
public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder) public static IHostBuilder UseWindowsService(this IHostBuilder hostBuilder)
{ {
return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder); return WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(hostBuilder);
} }
/// <summary> /// <summary>
/// 运行主机 /// 运行主机
/// </summary> /// </summary>
/// <param name="host"></param> /// <param name="app"></param>
/// <param name="singleton"></param> /// <param name="singleton"></param>
public static void Run(this IHost host, bool singleton = true) public static void Run(this WebApplication app, bool singleton)
{ {
var logger = host.Services.GetRequiredService<ILoggerFactory>().CreateLogger(nameof(FastGithub)); var logger = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger(nameof(FastGithub));
if (UseCommand(logger) == false) if (UseCommand(logger) == false)
{ {
using var mutex = new Mutex(true, "Global\\FastGithub", out var firstInstance); using var mutex = new Mutex(true, "Global\\FastGithub", out var firstInstance);
if (singleton == false || firstInstance) if (singleton == false || firstInstance)
{ {
HostingAbstractionsHostExtensions.Run(host); app.Run();
} }
else else
{ {