Program.cs 3.49 KB
using Autofac.Extensions.DependencyInjection;
using HHECS.Dal;
using HHECS.WebCommon.Config;
using HHECS.WebCommon.SystemHelp.Log;
using HHECS.WebCommon.Port;
using log4net;
using log4net.Config;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;

namespace HHECS.Web
{
    public class Program
    {
        #region 全局变量
        public static int httpPort { get; set; }
        public static int httpsPort { get; set; }
        #endregion

        public static void Main(string[] args)
        {
            #region Nlog
            InitLog4Net();
            #endregion

            InitSysValue();

            #region 端口是否占用
            var isHttpOk = ComputerHelp.PortInUse(httpPort);
            if (isHttpOk)
            {
                Console.WriteLine("端口httpPort已被占用:" + httpPort);
                return;
            }
            var isHttpsOk = ComputerHelp.PortInUse(httpsPort);
            if (isHttpsOk)
            {
                Console.WriteLine("端口httpsPort已被占用:" + httpsPort);
                return;
            }
            #endregion

            CreateHostBuilder(args).Build().Run();
        }


        public static IHostBuilder CreateHostBuilder(string[] args)
        {

            return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            }).UseServiceProviderFactory(new AutofacServiceProviderFactory());
        }

        private static void InitSysValue()
        {
            #region 获取配置信息        
            //添加 json 文件路径
            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json");
            //创建配置根对象
            var configurationRoot = builder.Build();
            //取 family 部分下的 mother 部分下的 name 部分
            var appCustomSettings = configurationRoot.GetSection("AppCustomSettings");

            DALHelper.Constr = configurationRoot.GetSection("ConnectionStrings").GetSection("BaseDBContext").Value;

            httpPort = int.Parse(appCustomSettings.GetSection("WebPort").Value);
            httpsPort = httpPort + 1000;
            #endregion
            string s = appCustomSettings.GetSection("LoginTitle").Value;
            ConfigRead.GetInstance.GetAppsetConnection().LoginTitle = appCustomSettings.GetSection("LoginTitle").Value;
            //内存缓存设置 路径和密码 定时器启动使用
            ConfigRead.GetInstance.GetAppsetConnection().HttpWebPort = httpPort;
            ConfigRead.GetInstance.GetAppsetConnection().HttpsWebPort = httpsPort;
        }

        private static void InitLog4Net()
        {
            //Log4NetHelper.Instance.Repository = LogManager.CreateRepository("NETCoreRepository");
            var log4Config = Path.Combine(Path.GetDirectoryName(typeof(Program).Assembly.Location), "Config", "log4net.config");
            if (!File.Exists(log4Config))
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("log4net.config文件路径不存在,请确认发布属性是【始终复制】:" + log4Config);
                //Log4NetHelper.Instance.Info("Certificate path:" + log4Config);
                Console.ReadLine();
            }
            //XmlConfigurator.Configure(Log4NetHelper.Instance.Repository, new FileInfo(log4Config));
            Console.WriteLine("InitLog4Net Init  Started successfully!");
        }
    }
}