/// <summary> /// /// </summary> /// <param name="args"></param> public static void Main(string[] args) { //增加commandline支持 var config = new ConfigurationBuilder() .SetBasePath(Path.Combine(AppContext.BaseDirectory, "Config")) .AddXmlFile("Config.config", optional: true) .AddXmlFile("Config_Local.config", optional: true) .AddCommandLine(args).Build(); string serverUrls = config.GetValue <string>("server.urls"); int port = GetPort(serverUrls); //window下程序参数中带有service=true表示以Windows服务运行方式 bool isService = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !(args.Contains("console=true") || Debugger.IsAttached); if (!isService && HostHelper.IsPortInUsed(port)) { //如果站点端口已经被占用 Console.WriteLine("端口已被占用,将关闭之前已启动程序"); CloseApp(port); } var host = WebHost.CreateDefaultBuilder(args).UseKestrel() .UseStartup <Startup>() .UseUrls(serverUrls) .Build(); if (isService) { host.RunAsService(); } else { host.Run(); } }
public void Configure() { IIocManager iocManager = IocManager.Instance; IAppInfoProvider appInfo = iocManager.Resolve <IAppInfoProvider>(); ILogger logger = iocManager.Resolve <ILogger <AfterRunConfigureModule> >(); IEnumerable <IAfterRunConfigure> configures = iocManager.Resolve <IEnumerable <IAfterRunConfigure> >(); if (configures == null && configures.Count() == 0) { return; } Task.Factory.StartNew(() => { int i = 0; while (!HostHelper.IsPortInUsed(Convert.ToInt32(appInfo.Ports[0]))) { try { foreach (var item in configures.OrderBy(e => e.Order)) { try { item.Configure(); } catch (Exception e) { logger.LogError(e.InnerException != null ? e.InnerException : e, $"调用类型{item.GetType().ToString()}的Configure方法异常"); } } break; } catch { } if (i > 30) { logger.LogError("程序启动异常,30s内http服务还未启动成功"); break; } i++; Task.Delay(100).Wait(); } }); }