Files
FA_WEB/FaKrosnoApi/Program.cs
2025-02-14 19:50:02 +01:00

109 lines
4.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using FaKrosnoApi.Models;
using FaKrosnoApi.Services;
using FaKrosnoEfDataModel;
using FaKrosnoEfDataModel.Services;
using Hangfire;
using Hangfire.SqlServer;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using OrdersManagementDataModel;
using OrdersManagementDataModel.Services;
using SytelineSaAppEfDataModel;
using SytelineSaAppEfDataModel.Services;
using FaKrosnoMappingProfile = FaKrosnoEfDataModel.MappingProfile;
using SytelineSaAppMappingProfile = SytelineSaAppEfDataModel.MappingProfile;
using OrdersManagementMappingProfile = OrdersManagementDataModel.MappingProfile;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<FaKrosnoDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("FaKrosnoConnection")));
builder.Services.AddDbContext<SytelineSaAppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("SytelineSaAppConnection")));
builder.Services.AddDbContext<OrdersManagementDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("OrdersManagementConnection")));
builder.Services.Configure<EmailSettingsModel>(builder.Configuration.GetSection("EmailSettings"));
builder.Services.Configure<JobSettingsModel>(builder.Configuration.GetSection("JobSettings"));
builder.WebHost.UseUrls("http://*:5001");
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApiDocument(config =>
{
config.Title = "FaKrosnoApi";
config.Version = "v1";
});
builder.Services.AddHangfire(config => config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(builder.Configuration.GetConnectionString("OrdersManagementConnection"), new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true
}));
builder.Services.AddHangfireServer();
// Configure AutoMapper
builder.Services.AddAutoMapper(typeof(FaKrosnoMappingProfile), typeof(SytelineSaAppMappingProfile),
typeof(OrdersManagementMappingProfile));
// Configure JWT Authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
};
});
builder.Services.AddScoped<IScheduleOrderService, ScheduleOrderService>();
builder.Services.AddScoped<IScheduleOrderDetailsService, ScheduleOrderDetailsService>();
builder.Services.AddScoped<IEdiCustomerOrderService, EdiCustomerOrderService>();
builder.Services.AddScoped<IErrorLogService, ErrorLogService>();
builder.Services.AddScoped<ICustomerOrderService, CustomerOrderService>();
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<IScheduleJobService, ScheduleJobService>();
builder.Services.AddScoped<ITaskSchedulerService, TaskSchedulerService>();
builder.Services.AddHostedService<TimedHostedService>();
var app = builder.Build();
app.UseOpenApi();
app.UseSwaggerUi();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.UseHangfireDashboard();
// var scopeFactory = app.Services.GetRequiredService<IServiceScopeFactory>();
// using (var scope = scopeFactory.CreateScope())
// {
// var scheduledJob = scope.ServiceProvider.GetRequiredService<IScheduleJobService>();
// scheduledJob.Start();
// }
app.Run();