refactor: Startup-Konfiguration in Program.cs verschoben, um das Setup zu vereinfachen
This commit is contained in:
parent
7ab24c696b
commit
3251d1214a
@ -1,64 +1,100 @@
|
||||
using HRD.AppLogger;
|
||||
using DAL;
|
||||
using HRD.AppLogger;
|
||||
using HRD.WebApi;
|
||||
using Microsoft.AspNetCore;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Web;
|
||||
using StaffDBServer.Extends;
|
||||
using StaffDBServer.SharedExtensions;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using HRD.LDAPService.JWT;
|
||||
using HRD.WebApi.DAL.Middleware;
|
||||
using HRD.WebApi.Helpers;
|
||||
|
||||
namespace StaffDBServer
|
||||
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) =>
|
||||
{
|
||||
public class Program
|
||||
ILoggerManager logger = new LoggerManager();
|
||||
logger.LogException((Exception)unhandledExceptionEventArgs.ExceptionObject, "Application closed due to exception.");
|
||||
NLog.LogManager.Flush();
|
||||
};
|
||||
|
||||
IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", true, true).Build();
|
||||
|
||||
WebApiConfig.Init(configuration, Assembly.GetExecutingAssembly().GetName());
|
||||
|
||||
ILoggerManager logger = new LoggerManager();
|
||||
logger.LogWarn($"[Start WebApi Server] BaseDirectory: {AppDomain.CurrentDomain.BaseDirectory}; TargetFrameworkName: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}");
|
||||
|
||||
try
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Configure logging
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.SetMinimumLevel(LogLevel.Warning);
|
||||
builder.Host.UseNLog();
|
||||
|
||||
// Add services to the container
|
||||
builder.Services.ConfigureWebApiExtensionsAtFirst(); // at first
|
||||
builder.Services.ConfigureRepositoryWrapper(); // add repos
|
||||
|
||||
var cnnStr = WebApiConfig.ConnectionString(EN_ConnectionType.SQLServer);
|
||||
|
||||
builder.Services.AddDbContext<WebApiContext>(options =>
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
const int dbTimeoutInMin = 5;
|
||||
options.UseSqlServer(cnnStr,
|
||||
opts => opts.CommandTimeout((int)TimeSpan.FromMinutes(dbTimeoutInMin).TotalSeconds));
|
||||
});
|
||||
|
||||
{
|
||||
AppDomain.CurrentDomain.UnhandledException += (object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs) =>
|
||||
{
|
||||
ILoggerManager logger = new LoggerManager();
|
||||
logger.LogException((Exception)unhandledExceptionEventArgs.ExceptionObject, "Application closed due to exception.");
|
||||
NLog.LogManager.Flush();
|
||||
};
|
||||
builder.Services.AddStaffDBRepositories();
|
||||
builder.Services.ConfigureWebApiExtensionsEnd(); // should come last
|
||||
|
||||
IConfiguration configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json", true, true).Build();
|
||||
var app = builder.Build();
|
||||
|
||||
WebApiConfig.Init(configuration, Assembly.GetExecutingAssembly().GetName());
|
||||
((IApplicationBuilder)app).ApplicationServices.SetupNLogServiceLocator();
|
||||
|
||||
ILoggerManager logger = new LoggerManager();
|
||||
logger.LogWarn($"[Start WebApi Server] BaseDirectory: {AppDomain.CurrentDomain.BaseDirectory}; TargetFrameworkName: {AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName}");
|
||||
|
||||
try
|
||||
{
|
||||
CreateWebHostBuilder(args)
|
||||
.Build()
|
||||
.Run();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogException(ex, "Stopped program because of exception");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
|
||||
NLog.LogManager.Flush();
|
||||
NLog.LogManager.Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
|
||||
.ConfigureLogging(options => options.ClearProviders())
|
||||
.UseStartup<Startup>().ConfigureLogging(logging =>
|
||||
{
|
||||
logging.ClearProviders();
|
||||
logging.SetMinimumLevel(LogLevel.Warning);
|
||||
})
|
||||
.UseNLog();
|
||||
// Configure the HTTP request pipeline
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
else
|
||||
{
|
||||
app.UseHsts();
|
||||
}
|
||||
|
||||
app.AddCustomExceptionHandling();
|
||||
app.UseHttpsRedirection();
|
||||
app.UseRouting();
|
||||
app.UseCors("AllowAllOrigins");
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
app.UseOpenApi();
|
||||
app.UseDALMiddleware();
|
||||
app.UseJwtMiddleware();
|
||||
app.ConfigureSwagger();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogException(ex, "Stopped program because of exception");
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
// Ensure to flush and stop internal timers/threads before application-exit (Avoid segmentation fault on Linux)
|
||||
NLog.LogManager.Flush();
|
||||
NLog.LogManager.Shutdown();
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user