feat(EnvelopeGenerator.Finalizer.Win): Create to process window-oriented services
This commit is contained in:
128
EnvelopeGenerator.Finalizer.Win/Program.cs
Normal file
128
EnvelopeGenerator.Finalizer.Win/Program.cs
Normal file
@@ -0,0 +1,128 @@
|
||||
using EnvelopeGenerator.DependencyInjection;
|
||||
using EnvelopeGenerator.Finalizer;
|
||||
using EnvelopeGenerator.Finalizer.Job;
|
||||
using EnvelopeGenerator.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Quartz;
|
||||
using Quartz.AspNetCore;
|
||||
using Quartzmon;
|
||||
using Serilog;
|
||||
|
||||
// Load Serilog from appsettings.json
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.Logging.json", optional: false, reloadOnChange: true)
|
||||
.Build())
|
||||
.CreateLogger();
|
||||
|
||||
try
|
||||
{
|
||||
Log.Information("Application is starting...");
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
#region Logging
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddSerilog();
|
||||
#endregion
|
||||
|
||||
#region Configuration
|
||||
var config = builder.Configuration;
|
||||
Directory
|
||||
.GetFiles(builder.Environment.ContentRootPath, "appsettings.*.json", SearchOption.TopDirectoryOnly)
|
||||
.Where(file => Path.GetFileName(file) != $"appsettings.Development.json")
|
||||
.Where(file => Path.GetFileName(file) != $"appsettings.migration.json")
|
||||
.ToList()
|
||||
.ForEach(file => config.AddJsonFile(file, true, true));
|
||||
#endregion
|
||||
|
||||
#region Web API Services
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
#endregion
|
||||
|
||||
#region Quartz
|
||||
builder.Services.AddQuartz(q =>
|
||||
{
|
||||
q.ScheduleJobDefault<CreateReportJob>(config);
|
||||
});
|
||||
|
||||
builder.Services.AddQuartzServer(options =>
|
||||
{
|
||||
options.WaitForJobsToComplete = true;
|
||||
});
|
||||
|
||||
builder.Services.AddQuartzmon();
|
||||
|
||||
builder.Services.AddSingleton(provider =>
|
||||
provider.GetRequiredService<ISchedulerFactory>().GetScheduler().Result
|
||||
);
|
||||
#endregion
|
||||
|
||||
#region Add DB Context, EG Inf. and Services
|
||||
var cnnStrName = "Default";
|
||||
var connStr = config.GetConnectionString(cnnStrName)
|
||||
?? throw new InvalidOperationException($"Connection string '{cnnStrName}' is missing in the application configuration.");
|
||||
|
||||
builder.Services.AddEnvelopeGenerator(egOptions => egOptions
|
||||
.AddLocalization()
|
||||
.AddDistributedSqlServerCache(options =>
|
||||
{
|
||||
options.ConnectionString = connStr;
|
||||
options.SchemaName = "dbo";
|
||||
options.TableName = "TBDD_CACHE";
|
||||
})
|
||||
.AddInfrastructure(opt =>
|
||||
{
|
||||
opt.AddDbTriggerParams(config);
|
||||
opt.AddDbContext((provider, options) =>
|
||||
{
|
||||
var logger = provider.GetRequiredService<ILogger<EGDbContext>>();
|
||||
var useInMemoryDb = config.GetValue<bool>("UseInMemoryDb");
|
||||
var dbCtxOpt = useInMemoryDb ? options.UseInMemoryDatabase("EGInMemoryDb") : options.UseSqlServer(connStr);
|
||||
dbCtxOpt.LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace)
|
||||
.EnableSensitiveDataLogging()
|
||||
.EnableDetailedErrors();
|
||||
});
|
||||
})
|
||||
.AddServices(config, true)
|
||||
);
|
||||
#endregion Add DB Context, EG Inf. and Services
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
#region Web API Middleware
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseQuartzmon(new QuartzmonOptions()
|
||||
{
|
||||
Scheduler = app.Services.GetRequiredService<IScheduler>(),
|
||||
VirtualPathRoot = "/quartz"
|
||||
});
|
||||
|
||||
app.MapControllers();
|
||||
#endregion
|
||||
|
||||
app.Run();
|
||||
|
||||
Log.Information("The worker was stopped.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal(ex, "Worker could not be started!");
|
||||
}
|
||||
finally
|
||||
{
|
||||
Log.CloseAndFlush();
|
||||
}
|
||||
Reference in New Issue
Block a user