Align .editorconfig files Move Controller logic to DMO classes GlobalVars.AppSettings = Models.AppSettings.GetFromConfigurationManager(); Question EditorConfig Project level editorconfig Format White Spaces AppSetting when EnvironmentVariable not set Corrective Actions Tests Schedule Actions Tests DMO Tests Controller Tests Get ready to use VSCode IDE
77 lines
3.6 KiB
C#
77 lines
3.6 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
|
|
using Fab2ApprovalSystem.Models;
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Hosting.WindowsServices;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Fab2ApprovalMKLink;
|
|
|
|
public class Program {
|
|
|
|
public static int Main(string[] args) {
|
|
ILogger<Program>? logger = null;
|
|
WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(args);
|
|
_ = webApplicationBuilder.Configuration.AddUserSecrets<Program>();
|
|
AppSettings appSettings = AppSettings.Get(webApplicationBuilder.Configuration);
|
|
if (string.IsNullOrEmpty(appSettings.Company))
|
|
throw new Exception("Company name must have a value!");
|
|
if (string.IsNullOrEmpty(appSettings.WorkingDirectoryName))
|
|
throw new Exception("Working directory name must have a value!");
|
|
try {
|
|
_ = webApplicationBuilder.Services.Configure<ApiBehaviorOptions>(options => options.SuppressModelStateInvalidFilter = true);
|
|
_ = webApplicationBuilder.Services.AddControllers();
|
|
_ = webApplicationBuilder.Services.AddDistributedMemoryCache();
|
|
_ = webApplicationBuilder.Services.AddHttpClient();
|
|
_ = webApplicationBuilder.Services.AddSingleton(_ => appSettings);
|
|
_ = webApplicationBuilder.Services.AddSwaggerGen();
|
|
_ = webApplicationBuilder.Services.AddSession(sessionOptions => {
|
|
sessionOptions.IdleTimeout = TimeSpan.FromSeconds(2000);
|
|
sessionOptions.Cookie.HttpOnly = true;
|
|
sessionOptions.Cookie.IsEssential = true;
|
|
}
|
|
);
|
|
if (WindowsServiceHelpers.IsWindowsService()) {
|
|
_ = webApplicationBuilder.Services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
|
|
_ = webApplicationBuilder.Logging.AddEventLog(settings => {
|
|
#pragma warning disable CA1416
|
|
if (string.IsNullOrEmpty(settings.SourceName))
|
|
settings.SourceName = webApplicationBuilder.Environment.ApplicationName;
|
|
#pragma warning restore
|
|
});
|
|
}
|
|
WebApplication webApplication = webApplicationBuilder.Build();
|
|
if (Debugger.IsAttached)
|
|
webApplication.Services.GetRequiredService<AppSettings>();
|
|
logger = webApplication.Services.GetRequiredService<ILogger<Program>>();
|
|
_ = webApplication.UseCors(corsPolicyBuilder => corsPolicyBuilder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
|
|
if (!webApplicationBuilder.Environment.IsDevelopment()) {
|
|
_ = webApplication.UseExceptionHandler("/Error");
|
|
_ = webApplication.UseHttpsRedirection();
|
|
_ = webApplication.UseHsts();
|
|
} else {
|
|
if (string.IsNullOrEmpty(appSettings.URLs)) {
|
|
Environment.ExitCode = -1;
|
|
webApplication.Lifetime.StopApplication();
|
|
}
|
|
_ = webApplication.UseSwagger();
|
|
_ = webApplication.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "Server V1"));
|
|
}
|
|
_ = webApplication.UseSession();
|
|
_ = webApplication.MapControllers();
|
|
logger.LogInformation("Starting Web Application");
|
|
webApplication.Run();
|
|
return 0;
|
|
} catch (Exception ex) {
|
|
try { logger?.LogCritical(ex, "Host terminated unexpectedly"); } catch (Exception) { }
|
|
throw;
|
|
}
|
|
}
|
|
|
|
} |