158 lines
5.0 KiB
C#
158 lines
5.0 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;
|
|
using System.IO;
|
|
using IRaCIS.Core.Domain.Share;
|
|
|
|
namespace IRaCIS.Core.API
|
|
{
|
|
public class Program
|
|
{
|
|
public readonly string environment;
|
|
public static async Task Main(string[] args)
|
|
{
|
|
try
|
|
{
|
|
//以配置文件为准,否则 从url中取环境值(服务以命令行传递参数启动,配置文件配置了就不需要传递环境参数)
|
|
var config = new ConfigurationBuilder()
|
|
.AddEnvironmentVariables()
|
|
.Build();
|
|
|
|
var enviromentName = config["ASPNETCORE_ENVIRONMENT"];
|
|
|
|
if (string.IsNullOrWhiteSpace(enviromentName))
|
|
{
|
|
|
|
var index = Array.IndexOf(args, "--env");
|
|
enviromentName = index > -1
|
|
? args[index + 1]
|
|
: "Development";
|
|
}
|
|
|
|
//Dicom 浏览
|
|
//ImageManager.SetImplementation(WinFormsImageManager.Instance);
|
|
|
|
var host = CreateHostBuilder(args)
|
|
.UseEnvironment(enviromentName) //命令行传入环境
|
|
.ConfigureAppConfiguration((hostContext, config) =>
|
|
{
|
|
|
|
//Console.WriteLine(hostContext.HostingEnvironment.EnvironmentName);
|
|
config.AddJsonFile("appsettings.json", false, true)
|
|
.AddJsonFile($"appsettings.{enviromentName}.json", false, true);
|
|
})
|
|
.Build();
|
|
|
|
|
|
//// Serilog
|
|
SerilogExtension.AddSerilogSetup(enviromentName, host.Services);
|
|
Log.Logger.Warning($"当前环境:{enviromentName}");
|
|
|
|
|
|
|
|
NewId.SetProcessIdProvider(new CurrentProcessIdProvider());
|
|
|
|
|
|
//缓存项目的状态 匿名化数据
|
|
await InitCache(host);
|
|
|
|
WatchJsonFile(Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, StaticData.En_US_Json) );
|
|
|
|
WatchJsonFile(Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources, StaticData.Zh_CN_Json));
|
|
|
|
|
|
|
|
|
|
host.Run();
|
|
|
|
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
|
|
Log.Logger.Error(e.InnerException is null ? e.Message + e.StackTrace : e.InnerException?.Message + e.InnerException?.StackTrace);
|
|
}
|
|
|
|
}
|
|
|
|
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());
|
|
|
|
|
|
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());
|
|
}
|
|
|
|
|
|
|
|
private static void LoadJsonFile(string filePath)
|
|
{
|
|
|
|
IConfigurationBuilder builder = new ConfigurationBuilder()
|
|
.AddJsonFile(filePath);
|
|
|
|
IConfigurationRoot enConfiguration = builder.Build();
|
|
|
|
|
|
|
|
foreach (IConfigurationSection section in enConfiguration.GetChildren())
|
|
{
|
|
if (filePath.Contains(StaticData.En_US_Json) )
|
|
{
|
|
StaticData.En_US_Dic[section.Key] = section.Value;
|
|
|
|
}
|
|
else
|
|
{
|
|
StaticData.Zh_CN_Dic[section.Key] = section.Value;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
public static void WatchJsonFile(string filePath)
|
|
{
|
|
LoadJsonFile(filePath);
|
|
|
|
|
|
FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath));
|
|
watcher.Changed += (sender, e) => LoadJsonFile(filePath);
|
|
watcher.EnableRaisingEvents = true;
|
|
|
|
}
|
|
|
|
}
|
|
}
|