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
This commit is contained in:
Arnaud Dartois committed 2026-07-20 19:58:32 +00:00
1 parent 947414cc3f
commit 34037c20bf
3 files changed
+18 -20

No files matched your search

@@ -72,6 +72,7 @@ public class AppDbContext : IdentityDbContext<AppUser, AppRole, Guid>
builder.Entity<CameraAssignment>().HasQueryFilter(e => e.Agent == null || !e.Agent.IsDeleted);
builder.Entity<CameraDiscoveryResult>().HasQueryFilter(e => e.Site == null || !e.Site.IsDeleted);
builder.Entity<CameraEvent>().HasQueryFilter(e => e.Camera == null || !e.Camera.IsDeleted);
builder.Entity<DiscoveredStreamUrl>().HasQueryFilter(e => e.DiscoveryResult == null || e.DiscoveryResult.Site == null || !e.DiscoveryResult.Site.IsDeleted);
// Group soft-delete filters
builder.Entity<Group>().HasQueryFilter(e => !e.IsDeleted);
@@ -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);
}
+16 -19
View File
@@ -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 ─────────────────────────────────