🐛 Fixes DI and lifetimes

This commit is contained in:
2025-08-20 01:41:27 +08:00
parent 9e8363c004
commit 1e8e2e9ea7
3 changed files with 12 additions and 5 deletions

View File

@@ -9,8 +9,7 @@ namespace DysonNetwork.Drive.Startup;
public class BroadcastEventHandler( public class BroadcastEventHandler(
INatsConnection nats, INatsConnection nats,
ILogger<BroadcastEventHandler> logger, ILogger<BroadcastEventHandler> logger,
FileService fs, IServiceProvider serviceProvider
AppDatabase db
) : BackgroundService ) : BackgroundService
{ {
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -23,7 +22,11 @@ public class BroadcastEventHandler(
if (evt == null) continue; if (evt == null) continue;
logger.LogInformation("Account deleted: {AccountId}", evt.AccountId); logger.LogInformation("Account deleted: {AccountId}", evt.AccountId);
using var scope = serviceProvider.CreateScope();
var fs = scope.ServiceProvider.GetRequiredService<FileService>();
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
await using var transaction = await db.Database.BeginTransactionAsync(cancellationToken: stoppingToken); await using var transaction = await db.Database.BeginTransactionAsync(cancellationToken: stoppingToken);
try try
{ {

View File

@@ -4,6 +4,7 @@ using DysonNetwork.Pass.Startup;
using DysonNetwork.Shared.Http; using DysonNetwork.Shared.Http;
using DysonNetwork.Shared.PageData; using DysonNetwork.Shared.PageData;
using DysonNetwork.Shared.Registry; using DysonNetwork.Shared.Registry;
using DysonNetwork.Shared.Stream;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@@ -16,6 +17,7 @@ builder.Services.AddAppMetrics();
// Add application services // Add application services
builder.Services.AddRegistryService(builder.Configuration); builder.Services.AddRegistryService(builder.Configuration);
builder.Services.AddStreamConnection(builder.Configuration);
builder.Services.AddAppServices(builder.Configuration); builder.Services.AddAppServices(builder.Configuration);
builder.Services.AddAppRateLimiting(); builder.Services.AddAppRateLimiting();
builder.Services.AddAppAuthentication(); builder.Services.AddAppAuthentication();

View File

@@ -8,7 +8,7 @@ namespace DysonNetwork.Sphere.Startup;
public class BroadcastEventHandler( public class BroadcastEventHandler(
INatsConnection nats, INatsConnection nats,
ILogger<BroadcastEventHandler> logger, ILogger<BroadcastEventHandler> logger,
AppDatabase db IServiceProvider serviceProvider
) : BackgroundService ) : BackgroundService
{ {
protected override async Task ExecuteAsync(CancellationToken stoppingToken) protected override async Task ExecuteAsync(CancellationToken stoppingToken)
@@ -22,7 +22,9 @@ public class BroadcastEventHandler(
logger.LogInformation("Account deleted: {AccountId}", evt.AccountId); logger.LogInformation("Account deleted: {AccountId}", evt.AccountId);
// TODO: Add empty realm, chat recycler in the db recycle using var scope = serviceProvider.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDatabase>();
await db.ChatMembers await db.ChatMembers
.Where(m => m.AccountId == evt.AccountId) .Where(m => m.AccountId == evt.AccountId)
.ExecuteDeleteAsync(cancellationToken: stoppingToken); .ExecuteDeleteAsync(cancellationToken: stoppingToken);