103 lines
4.3 KiB
C#
103 lines
4.3 KiB
C#
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;
|
|
using OI.Metrology.Archive.Repositories;
|
|
using OI.Metrology.Archive.Services;
|
|
using OI.Metrology.Shared.Repositories;
|
|
using OI.Metrology.Shared.Services;
|
|
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
|
|
namespace OI.Metrology.Archive;
|
|
|
|
public class Program
|
|
{
|
|
|
|
private static string GetWebRoot()
|
|
{
|
|
string result;
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
string assemblyName = assembly.GetName()?.Name;
|
|
if (string.IsNullOrEmpty(assemblyName))
|
|
throw new Exception();
|
|
string net6 = "net6.0";
|
|
string baseAssemblyName = assemblyName.Split('.')[0];
|
|
if (WindowsServiceHelpers.IsWindowsService())
|
|
result = Path.Combine("D:", net6, baseAssemblyName, "wwwroot");
|
|
else
|
|
result = Path.Combine(AppContext.BaseDirectory.Split(baseAssemblyName)[0], baseAssemblyName, "Client", "bin", "Debug", net6, "wwwroot");
|
|
if (!Directory.Exists(result))
|
|
result = string.Empty;
|
|
return result;
|
|
}
|
|
|
|
public static int Main(string[] args)
|
|
{
|
|
WebApplicationOptions options = new()
|
|
{
|
|
Args = args,
|
|
ContentRootPath = AppContext.BaseDirectory,
|
|
WebRootPath = GetWebRoot()
|
|
};
|
|
WebApplicationBuilder webApplicationBuilder = WebApplication.CreateBuilder(options);
|
|
_ = webApplicationBuilder.Configuration.AddUserSecrets<Program>();
|
|
try
|
|
{
|
|
// this prevents validation errors from being handled by ASP.NET and not hitting our custom error handle
|
|
_ = webApplicationBuilder.Services.Configure<ApiBehaviorOptions>(options => options.SuppressModelStateInvalidFilter = true);
|
|
_ = webApplicationBuilder.Services.AddControllersWithViews();
|
|
_ = new MetrologyRepo(new SQLDbConnectionFactory(webApplicationBuilder.Configuration), null);
|
|
_ = webApplicationBuilder.Services.AddSingleton<IDbConnectionFactory, SQLDbConnectionFactory>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IMetrologyRepo, MetrologyRepo>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IAttachmentsService, AttachmentsService>();
|
|
_ = webApplicationBuilder.Services.AddScoped<IInboundDataService, InboundDataService>();
|
|
_ = webApplicationBuilder.Services.AddMemoryCache();
|
|
_ = webApplicationBuilder.Services.AddDistributedMemoryCache();
|
|
_ = webApplicationBuilder.Services.AddSession(sessionOptions =>
|
|
{
|
|
// Set a short timeout for easy testing.
|
|
sessionOptions.IdleTimeout = TimeSpan.FromSeconds(2000);
|
|
sessionOptions.Cookie.HttpOnly = true;
|
|
// Make the session cookie essential
|
|
sessionOptions.Cookie.IsEssential = true;
|
|
}
|
|
);
|
|
if (WindowsServiceHelpers.IsWindowsService())
|
|
{
|
|
_ = webApplicationBuilder.Services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
|
|
_ = webApplicationBuilder.Logging.AddEventLog(settings =>
|
|
{
|
|
if (string.IsNullOrEmpty(settings.SourceName))
|
|
settings.SourceName = webApplicationBuilder.Environment.ApplicationName;
|
|
});
|
|
}
|
|
WebApplication webApplication = webApplicationBuilder.Build();
|
|
if (webApplicationBuilder.Environment.IsDevelopment())
|
|
{
|
|
_ = webApplication.UseExceptionHandler("/Error");
|
|
_ = webApplication.UseHsts();
|
|
}
|
|
_ = webApplication.UseStaticFiles();
|
|
_ = webApplication.UseSession();
|
|
_ = webApplication.UseHttpsRedirection();
|
|
_ = webApplication.UseMiddleware<ApiLoggingMiddleware>();
|
|
_ = webApplication.MapControllers();
|
|
webApplication.Run();
|
|
return 0;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
return 1;
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
|
|
} |