using DB.HubXDB; using HubX.Library.Config; using HubX.Library.Socket.Session; using HubX.Server; using HubX.Server.Services; using HubX.Server.TaskManager; using Microsoft.EntityFrameworkCore; using System.Net; using System.Net.Sockets; using SystemX.Core.Communication; using SystemX.Core.DB; using SystemX.Core.Services; string configDir = @"../Config"; //raed log4net config if (Log4net.IsConfigLoad == true) { Log4net.WriteLine("Log4net Init Success"); } else { Log4net.WriteLine("Log4net Init Failed", LogType.Error); } var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddHttpContextAccessor(); //singleton builder.Services.AddSingleton>(); //scoped builder.Services.AddSingleton(); builder.Services.AddSingleton(); //config preload ConfigService preloadConfig = new ConfigService(); if (preloadConfig.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true) { var config = preloadConfig.GetConfig(); } else { Console.WriteLine("Config Preload Load Error."); return; } builder.Services.AddDbContext(); var app = builder.Build(); //read api config and set string serverUrl = string.Empty; var configService = app.Services.GetService>(); bool isIIS = false; int socketPort = 0; if (configService?.OpenConfig($@"{configDir}/HubX.WebApiConfig.json") == true) { Log4net.WriteLine("WebApi Config Success."); var apiConfig = ConfigService.Config; if (apiConfig != null) { serverUrl = $"{apiConfig.Server.Address}:{apiConfig.Server.Port}"; isIIS = apiConfig.Server.IIS; //socket socketPort = apiConfig.Socket.Port; } } else { Log4net.WriteLine("WebApi Config Error."); return; } // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { Log4net.WriteLine($"IsDevelopment:{app.Environment.IsDevelopment()}"); Log4net.WriteLine($"Swagger Url: {serverUrl}/swagger"); app.UseSwagger(); app.UseSwaggerUI(); } TaskSocket taskSocket = new TaskSocket(); taskSocket?.Run(socketPort); app.UseAuthentication(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); if (isIIS == true) { app.Run(); } else { Log4net.WriteLine($"Operation Url: {serverUrl}"); app.Run($"{serverUrl}"); }