159 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			159 lines
		
	
	
		
			5.7 KiB
		
	
	
	
		
			C#
		
	
	
| using System;
 | ||
| using Autofac.Extensions.DependencyInjection;
 | ||
| using Microsoft.AspNetCore.Hosting;
 | ||
| using Microsoft.Extensions.Hosting;
 | ||
| using Microsoft.Extensions.Configuration;
 | ||
| using Serilog;
 | ||
| using MediatR;
 | ||
| using IRaCIS.Core.Application.MediatR.Handlers;
 | ||
| using System.Threading.Tasks;
 | ||
| using MassTransit;
 | ||
| using MassTransit.NewIdProviders;
 | ||
| 
 | ||
| namespace IRaCIS.Core.API
 | ||
| {
 | ||
|     public class Program
 | ||
|     {
 | ||
|         public readonly string environment;
 | ||
|         public static async Task Main(string[] args)
 | ||
|         {
 | ||
|             try
 | ||
|             {
 | ||
|                 var index = Array.IndexOf(args, "--env");
 | ||
|                 var environment = index > -1
 | ||
|                      ? args[index + 1]
 | ||
|                      : "Development";
 | ||
| 
 | ||
|                 //Dicom 浏览
 | ||
|                 //ImageManager.SetImplementation(WinFormsImageManager.Instance);
 | ||
| 
 | ||
|                 var host = CreateHostBuilder(args)
 | ||
|                     .UseEnvironment(environment) //命令行传入环境
 | ||
|                     .ConfigureAppConfiguration((hostContext, config) =>
 | ||
|                     {
 | ||
| 
 | ||
|                         //Console.WriteLine(hostContext.HostingEnvironment.EnvironmentName);                
 | ||
|                         config.AddJsonFile("appsettings.json", false, true)
 | ||
|                                   .AddJsonFile($"appsettings.{environment}.json", false, true);
 | ||
|                     })
 | ||
|                     .Build();
 | ||
| 
 | ||
|                 #region Id Generate  long类型
 | ||
| 
 | ||
| 
 | ||
|                 //// 创建 IdGeneratorOptions 对象,请在构造函数中输入 WorkerId:
 | ||
|                 //var options = new IdGeneratorOptions(1);
 | ||
|                 ////options.WorkerIdBitLength = 10;
 | ||
| 
 | ||
|                 //YitIdHelper.SetIdGenerator(options);
 | ||
| 
 | ||
|                 //var newId = YitIdHelper.NextId();
 | ||
| 
 | ||
|                 #endregion
 | ||
| 
 | ||
|                 NewId.SetProcessIdProvider(new CurrentProcessIdProvider());
 | ||
| 
 | ||
|                 for (int i= 0; i < 10;i++ )
 | ||
|                 {
 | ||
|                     Console.WriteLine(NewId.NextGuid());
 | ||
|                 }
 | ||
| 
 | ||
| 
 | ||
|                 //// Serilog 
 | ||
|                 SerilogExtension.AddSerilogSetup(environment, host.Services);
 | ||
| 
 | ||
|                 //缓存项目的状态  匿名化数据
 | ||
|                 await InitCache(host);
 | ||
| 
 | ||
|                 //Log.Logger.Error("缓存项目状态完毕");
 | ||
| 
 | ||
|                 host.Run();
 | ||
|             }
 | ||
|             catch (Exception e)
 | ||
|             {
 | ||
| 
 | ||
|                 Log.Logger.Error(e.InnerException is null ? e.Message + e.StackTrace : e.InnerException?.Message + e.InnerException?.StackTrace);
 | ||
|             }
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|             #region Nlog 废弃
 | ||
|             //var logger = NLog.Web.NLogBuilder.ConfigureNLog("NLog.config").GetCurrentClassLogger();
 | ||
|             //try
 | ||
|             //{
 | ||
|             //    var ihostBuilder = CreateHostBuilder(args);
 | ||
| 
 | ||
|             //    ihostBuilder.UseEnvironment(environment);
 | ||
| 
 | ||
|             //    ihostBuilder.ConfigureAppConfiguration((hostContext, config) =>
 | ||
|             //    {
 | ||
| 
 | ||
|             //        //Console.WriteLine(hostContext.HostingEnvironment.EnvironmentName);                
 | ||
|             //        config.AddJsonFile("appsettings.json", false, true)
 | ||
|             //              .AddJsonFile($"appsettings.{environment}.json", false, true);
 | ||
|             //    });
 | ||
| 
 | ||
|             //    var host = ihostBuilder.Build();
 | ||
| 
 | ||
|             //    CacheTrialStatus(host);
 | ||
| 
 | ||
|             //    host.Run();
 | ||
| 
 | ||
|             //}
 | ||
|             //catch (Exception exception)
 | ||
|             //{
 | ||
|             //    //NLog: catch setup errors
 | ||
|             //    logger.Error(exception, "Stopped program because of exception");
 | ||
|             //}
 | ||
|             //finally
 | ||
|             //{
 | ||
|             //    // Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
 | ||
|             //    NLog.LogManager.Shutdown();
 | ||
|             //}
 | ||
| 
 | ||
|             #endregion
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|         }
 | ||
| 
 | ||
|         public static IHostBuilder CreateHostBuilder(string[] args) =>
 | ||
|             Host.CreateDefaultBuilder(args)
 | ||
|                 .UseWindowsService()
 | ||
|                 .ConfigureWebHostDefaults(webBuilder =>
 | ||
|                 {
 | ||
|                     webBuilder.ConfigureKestrel((context, options) =>
 | ||
|                      {
 | ||
|                          //设置应用服务器Kestrel请求体最大为1GB // if don't set default value is: 30 MB
 | ||
|                          options.Limits.MaxRequestBodySize = long.MaxValue;
 | ||
|                          options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);
 | ||
|                          options.Limits.RequestHeadersTimeout = TimeSpan.FromMinutes(20);
 | ||
| 
 | ||
|                      });
 | ||
|                     //webBuilder.UseSerilog();//在宿主机启动的时候配置serilog,与微软ILogger进行整合
 | ||
|                     webBuilder.UseStartup<Startup>();
 | ||
|                 }).UseSerilog()
 | ||
|                 .UseServiceProviderFactory(new AutofacServiceProviderFactory());//使用Autofac替代本身容器
 | ||
|                                                                                 //ConfigureLogging(logging =>
 | ||
|                                                                                 //{
 | ||
|                                                                                 //    logging.ClearProviders();
 | ||
|                                                                                 //    logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
 | ||
|                                                                                 //})
 | ||
|                                                                                 //.UseNLog()
 | ||
|                                                                                 // NLog: Setup NLog for Dependency injection;
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
|         private static async Task InitCache(IHost host)
 | ||
|         {
 | ||
|             var _mediator = host.Services.GetService(typeof(IMediator)) as IMediator;
 | ||
| 
 | ||
|             await _mediator.Send(new AnonymizeCacheRequest());
 | ||
| 
 | ||
|             await _mediator.Send(new TrialStateCacheRequest());
 | ||
|         }     
 | ||
| 
 | ||
|     }
 | ||
| }
 |