using AspectCore.Extensions.DependencyInjection; using HealthMonitor.Common; using HealthMonitor.Common.helper; using HealthMonitor.Core.Cache; using HealthMonitor.Core.Context; using HealthMonitor.Core.Dal.EfCoreImpl; using HealthMonitor.Core.Dal; using HealthMonitor.Core.Dal.Factory; using HealthMonitor.Core.DbLog; using HealthMonitor.Core.Operator; using HealthMonitor.Core.Operator.Redis; using HealthMonitor.Model.Config; using HealthMonitor.Service.Biz.db; using HealthMonitor.WebApi.Configs; using HealthMonitor.WebApi.DbLog; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using HealthMonitor.WebApi.Swagger; using HealthMonitor.Service.Cache; using TelpoDataService.Util.Clients; using HealthMonitor.Service.Sub; using HealthMonitor.Service.Resolver; using HealthMonitor.Service.Resolver.Factory; using HealthMonitor.Service.Resolver.Interface; using Serilog; using Serilog.Core; using HealthMonitor.WebApi.HttpLog; using Microsoft.Extensions.Http; using Microsoft.Extensions.DependencyInjection.Extensions; using HealthMonitor.Service.Etcd; using HealthMonitor.WebApi.Middleware; namespace HealthMonitor.WebApi { public class Program { public static void Main(string[] args) { //选择配置文件appsetting.json var config = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .Build(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(config).Enrich.WithThreadInfo() .CreateLogger(); var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddHttpClient(Consts.DEFAULT_HTTPCLIENT_NAME, c => { c.Timeout = TimeSpan.FromSeconds(10); //超时限制 c.DefaultRequestHeaders.Add("Accept", "application/json"); //c.DefaultRequestHeaders.Connection.Add("keep-alive"); }); // builder.Services.Configure(builder.Configuration.GetSection("TDengineServiceConfig")); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); #region scoped MySQL 数据库 builder.Services // gps_card .AddDbContextPool((sp, options) => { var loggerFactory = sp.GetRequiredService(); var mySqlCon = builder.Configuration.GetConnectionString("GpsCard_Connection_String"); var serverVersion = ServerVersion.AutoDetect(mySqlCon); options.UseMySql(mySqlCon, serverVersion) .UseLoggerFactory(loggerFactory); }, poolSize: 64) // health_monitor .AddDbContextPool((sp, options) => { var loggerFactory = sp.GetRequiredService(); var mySqlCon = builder.Configuration.GetConnectionString("HealthMonitor_Connection_String"); var serverVersion = ServerVersion.AutoDetect(mySqlCon); options.UseMySql(mySqlCon, serverVersion) .UseLoggerFactory(loggerFactory); }, poolSize: 64); ; builder.Services .AddScoped(sp => { var context = sp.GetRequiredService(); return new EfCoreDataAccessor(context); }) .AddScoped(sp => { var context = sp.GetRequiredService(); return new EfCoreDataAccessor(context); }); //builder.Services.AddDbContextPool((sp, options) => //{ // var loggerFactory = sp.GetRequiredService(); // options.UseMySql(builder.Configuration.GetConnectionString("GpsCard_Connection_String")) // .UseLoggerFactory(loggerFactory); //}, poolSize: 64); //builder.Services.AddScoped(sp => //{ // var context = sp.GetRequiredService(); // return new EfCoreDataAccessor(context); //}); #endregion #region AOP //builder.Services.Configure(builder.Configuration.GetSection("Redis")); builder.Services .Configure(options => { options.SuppressModelStateInvalidFilter = true; }) .Configure(builder.Configuration.GetSection("Redis")) .Configure(builder.Configuration.GetSection("TDengineServiceConfig")) .Configure(builder.Configuration.GetSection("ServiceConfig")); builder.Services .AddSingleton() .AddSingleton() .AddSingleton(sp => { var provider = sp.GetRequiredService(); return new EfCoreLoggerFactory(new[] { provider }); }); //builder.Services.AddSingleton(); //builder.Services.AddSingleton(sp => //{ // var provider = sp.GetRequiredService(); // return new EfCoreLoggerFactory(new[] { provider }); //}); builder.Services .AddSingleton() .AddSingleton(); //builder.Services.AddSingleton(); builder.Services .AddSingleton() .AddSingleton(); //builder.Services.AddSingleton(); //用AspectCore替换默认的IOC容器 builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory()); #endregion #region Cache builder.Services .AddSingleton() .AddSingleton(); #endregion #region TelpoDataServices builder.Services.AddTelpoDataServices(opt => { opt.TelpoDataUrl = builder.Configuration.GetSection("ServiceConfig:TelpoDataUrl").Value; }); #endregion #region Worker builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services .AddSingleton() .AddSingleton() .AddHostedService(); #endregion builder.Host.UseSerilog(); builder.Services.Replace(ServiceDescriptor.Singleton()); // Register the Swagger generator, defining 1 or more Swagger documents builder.Services.AddSwaggerGen(c => { //c.SwaggerDoc(AppConsts.SWAGGER_DOC_GpsCard, new OpenApiInfo { Title = "GpsCard模块", Version = "v1", Description = "gps_card数据库服务" }); //分组显示 c.SwaggerDoc(AppConsts.SWAGGER_DOC_HealthMonitor, new OpenApiInfo { Title = "HealthMonitor模块", Version = "v1", Description = "health_monitor数据库服务" }); //分组显示 c.DocumentFilter(); //var utilXmlFile = Path.Combine(AppContext.BaseDirectory, "HealthMonitor.Util.xml"); //var webapiXmlFile = Path.Combine(AppContext.BaseDirectory, "HealthMonitor.WebApi.xml"); //c.IncludeXmlComments(utilXmlFile, true); //c.IncludeXmlComments(webapiXmlFile, true); }); var app = builder.Build(); // Configure the HTTP request pipeline. //if (app.Environment.IsDevelopment()) //{ // app.UseSwagger(); // app.UseSwaggerUI(); //} string prefix = string.Empty; //if (!app.Environment.IsDebugOrDevelop()) //{ // prefix = optConfigAppSettings.Value.NginxPrefix; // if (string.IsNullOrWhiteSpace(prefix)) prefix = SERVICE_PREFIX; //} // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseSwagger(c => { c.PreSerializeFilters.Add((swagger, httpReq) => { var scheme = httpReq.Headers.TryGetValue("X-Forwarded-Proto", out var v2) ? v2.ToString() : httpReq.Scheme; swagger.Servers = new List { new OpenApiServer { Url = $"{scheme}://{httpReq.Host}/{prefix}" } }; }); c.RouteTemplate = "/{documentName}/api-docs/"; }); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { string sub = string.IsNullOrEmpty(prefix) ? "" : $"/{prefix}"; //c.SwaggerEndpoint($"{sub}/{AppConsts.SWAGGER_DOC_GpsCard}/api-docs/", "GpsCard模块"); //分组显示 c.SwaggerEndpoint($"{sub}/{AppConsts.SWAGGER_DOC_HealthMonitor}/api-docs/", "HealthMonitor模块"); //分组显示 c.RoutePrefix = string.Empty; }); // redis default var csredis = new CSRedis.CSRedisClient(app.Services.GetService>()!.Value.ToString()); RedisHelper.Initialization(csredis); // redis db7 var csredisDb7Con = app.Services.GetService>()!.Value; csredisDb7Con.DefaultDatabase = 7; csredisDb7Con.Prefix = "TELPO"; var csredisDb7 = new CSRedis.CSRedisClient(csredisDb7Con.ToString()); RedisHelperDb7.Initialization(csredisDb7); app.UseHttpsRedirection(); app.UseAuthorization(); app.UseMiddleware(); app.MapControllers(); app.Run(); } } }