-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
98 lines (82 loc) · 3.23 KB
/
Program.cs
File metadata and controls
98 lines (82 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using StudentEnrollmentSystem1.Data;
using StudentEnrollmentSystem1.Models;
using System;
using System.Threading.Tasks;
using static System.Formats.Asn1.AsnWriter;
// Make the Program class async
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
// Configure MySQL database connection
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDatabaseServices(connectionString);
// Enable sensitive data logging in development
if (builder.Environment.IsDevelopment())
{
builder.Services.Configure<DbContextOptionsBuilder>(options =>
{
options.EnableSensitiveDataLogging();
});
}
// Add session support
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(30);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
else
{
app.UseDeveloperExceptionPage();
}
// Initialize and seed the database
try
{
using var scope = app.Services.CreateScope();
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Initializing database...");
await DbInitializer.InitializeAsync(app.Services, logger);
logger.LogInformation("Database initialization completed successfully.");
Console.WriteLine("Database initialized successfully.");
}
catch (Exception ex)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogError(ex, "An error occurred while initializing the database.");
Console.WriteLine($"Database initialization error: {ex.Message}");
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();
// Map static assets if the method exists (for compatibility)
if (app.GetType().GetMethod("MapStaticAssets") != null)
{
app.GetType().GetMethod("MapStaticAssets").Invoke(app, null);
}
Console.WriteLine("Application started. Press Ctrl+C to shut down.");
await app.RunAsync();
}
}