| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using Microsoft.AspNetCore.Hosting;
- using System.Runtime.InteropServices;
- using WCS.Core;
- using WCS.WebApi;
- using RedisHelper = WCS.Core.Redis.RedisHelper;
- namespace WCS.Service
- {
- public class Program
- {
- /// <summary>
- /// 程序入口
- /// </summary>
- /// <param name="args"></param>
- public static void Main(string[] args)
- {
- #region 接入Redis
- RedisHelper.CreateContext(AppSettings.Config.GetConnectionString("Redis"), "default", true);
- #endregion 接入Redis
- //互斥锁检测
- var mutexName = RedisHelper.Default.Get("Mutex");
- if (string.IsNullOrEmpty(mutexName))
- {
- RedisHelper.Default.Set("Mutex", " ");
- Console.WriteLine("请在Redis中配置互斥量值");
- return;
- }
- using var mt = new Mutex(true, mutexName);
- if (mt.WaitOne())
- {
- Configs.AddSystemMode(SystemMode.虚拟plc);
- CreateHostBuilder(args).Build().Run(); mt.ReleaseMutex();
- }
- else
- {
- Console.WriteLine("请勿重复运行");
- //InfoLog.INFO_INIT("请勿重复运行");
- Task.Delay(2000).Wait();
- }
- }
- /// <summary>
- /// 创建一个主机构建器
- /// </summary>
- /// <param name="args"></param>
- /// <returns></returns>
- public static IHostBuilder CreateHostBuilder(string[] args)
- {
- //是否是win平台
- var isWin = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
- Console.WriteLine($"win:{isWin}");
- if (isWin)
- {
- return Host.CreateDefaultBuilder(args)
- .UseWindowsService()//win
- .ConfigureWebHostDefaults(web => //网络访问配置
- {
- web.UseUrls("http://*:8080"); //设备访问端口
- web.UseStartup<Startup>(); //调用启动服务
- })
- .ConfigureServices((_, services) =>
- {
- //services.AddDbContext<WCSDB>();
- services.AddHostedService<Worker>();
- services.AddHttpClient();
- });
- }
- return Host.CreateDefaultBuilder(args)
- .UseSystemd()//linux
- .ConfigureServices((_, services) =>
- {
- services.AddHostedService<Worker>();
- });
- }
- }
- }
|