1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using System;
- using System.Runtime.InteropServices;
- using System.Threading;
- using System.Threading.Tasks;
- using WCS.Core;
- namespace WCS.Service
- {
- public class Program
- {
- /// <summary>
- /// 程序入口
- /// </summary>
- /// <param name="args"></param>
- public static void Main(string[] args)
- {
- using var mt = new Mutex(true, "WCS");
- 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>();
- });
- }
- }
- }
|