From 34037c20bfc63fddf219f74cc4106aec5ec80284 Mon Sep 17 00:00:00 2001 From: Arnaud Dartois Date: Mon, 20 Jul 2026 19:58:32 +0000 Subject: [PATCH] fix(dp): keep X509Certificate2 alive for DataProtection key ring - Remove `using var` from dpCert so the cert stays alive for ProtectKeysWithCertificate (was disposed, causing CryptographicException) - Add query filter on DiscoveredStreamUrl for model warning #10622 - Simplify migration retry: call Migrate() directly instead of GetPendingMigrations()+Migrate() to reduce transient startup noise --- .../Data/AppDbContext.cs | 1 + .../DependencyInjection.cs | 2 +- src/Heimdall.Web/Program.cs | 35 +++++++++---------- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Heimdall.Infrastructure/Data/AppDbContext.cs b/src/Heimdall.Infrastructure/Data/AppDbContext.cs index 7b2442a..4b67cbb 100644 --- a/src/Heimdall.Infrastructure/Data/AppDbContext.cs +++ b/src/Heimdall.Infrastructure/Data/AppDbContext.cs @@ -72,6 +72,7 @@ public class AppDbContext : IdentityDbContext builder.Entity().HasQueryFilter(e => e.Agent == null || !e.Agent.IsDeleted); builder.Entity().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted); builder.Entity().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted); + builder.Entity().HasQueryFilter(e => e.DiscoveryResult == null || e.DiscoveryResult.Site == null || !e.DiscoveryResult.Site.IsDeleted); // Group soft-delete filters builder.Entity().HasQueryFilter(e => !e.IsDeleted); diff --git a/src/Heimdall.Infrastructure/DependencyInjection.cs b/src/Heimdall.Infrastructure/DependencyInjection.cs index 8c007d5..1ece446 100644 --- a/src/Heimdall.Infrastructure/DependencyInjection.cs +++ b/src/Heimdall.Infrastructure/DependencyInjection.cs @@ -106,7 +106,7 @@ public static class DependencyInjection } #pragma warning disable SYSLIB0057 // X509Certificate2(string, string) is obsolete — X509CertificateLoader not available in this SDK - using var dpCert = new X509Certificate2(certFile.FullName, ""); + var dpCert = new X509Certificate2(certFile.FullName, ""); #pragma warning restore SYSLIB0057 dpBuilder.ProtectKeysWithCertificate(dpCert); } diff --git a/src/Heimdall.Web/Program.cs b/src/Heimdall.Web/Program.cs index 88d323a..bcf2ad4 100644 --- a/src/Heimdall.Web/Program.cs +++ b/src/Heimdall.Web/Program.cs @@ -277,31 +277,28 @@ using (var scope = app.Services.CreateScope()) var retryDelay = TimeSpan.FromSeconds(2); for (var attempt = 1; attempt <= maxRetries; attempt++) - { - try { - if (db.Database.GetPendingMigrations().Any()) + try { - logger.LogInformation("Applying pending database migrations (attempt {Attempt}/{MaxRetries})...", attempt, maxRetries); + logger.LogInformation("Applying database migrations (attempt {Attempt}/{MaxRetries})...", attempt, maxRetries); db.Database.Migrate(); logger.LogInformation("Database migrations applied successfully"); + break; + } + catch (Exception ex) when (attempt < maxRetries) + { + logger.LogWarning(ex, + "Database migration attempt {Attempt}/{MaxRetries} failed. Retrying in {Delay}s...", + attempt, maxRetries, retryDelay.TotalSeconds); + await Task.Delay(retryDelay); + retryDelay = TimeSpan.FromSeconds(retryDelay.TotalSeconds * 2); + } + catch (Exception ex) + { + logger.LogCritical(ex, "Failed to apply database migrations after {MaxRetries} attempts.", maxRetries); + throw; } - break; } - catch (Exception ex) when (attempt < maxRetries) - { - logger.LogWarning(ex, - "Database migration attempt {Attempt}/{MaxRetries} failed. Retrying in {Delay}s...", - attempt, maxRetries, retryDelay.TotalSeconds); - await Task.Delay(retryDelay); - retryDelay = TimeSpan.FromSeconds(retryDelay.TotalSeconds * 2); - } - catch (Exception ex) - { - logger.LogCritical(ex, "Failed to apply database migrations after {MaxRetries} attempts.", maxRetries); - throw; - } - } } // ── Register recurring Hangfire jobs ─────────────────────────────────