using API.Middlewares; using Infrastructure; using Infrastructure.Data; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args); // Configure Kestrel with certificate paths from appsettings.json builder.WebHost.ConfigureKestrel((context, serverOptions) => { serverOptions.Configure(context.Configuration.GetSection("Kestrel"), reloadOnChange: true); }); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", builder => { builder.AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .WithExposedHeaders("*"); // This exposes all headers }); }); builder.Services.AddControllers(); builder.Services.AddInfrastructureServices(builder.Configuration); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.AddSecurityDefinition("ApiKey", new OpenApiSecurityScheme { Description = "ApiKey must appear in header", Type = SecuritySchemeType.ApiKey, Name = "ApiKey", In = ParameterLocation.Header, Scheme = "ApiKeyScheme" }); var key = new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "ApiKey" }, In = ParameterLocation.Header }; var requirement = new OpenApiSecurityRequirement { { key, new List() } }; c.AddSecurityRequirement(requirement); }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseCors("AllowAll"); app.UseHttpsRedirection(); app.MapControllers(); // Middlewares app.UseMiddleware(); app.UseMiddleware(); app.UseMiddleware(); using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; var dbContext = services.GetRequiredService(); // Directly resolving your DbContext EnsureDatabaseCreated(dbContext); } app.Run(); static void EnsureDatabaseCreated(HealthcareManagerDatabase dbContext) { // Checking for pending migrations is more efficient than applying migrations unconditionally if (dbContext.Database.GetPendingMigrations().Any()) dbContext.Database.Migrate(); }