From eff0c11f262da4a61c23801cdd9240d389a4d894 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Sun, 14 Dec 2025 01:24:24 +0700 Subject: [PATCH] Refactor Sentry initialization in Program.cs to conditionally set DSN based on configuration; enhance DiscordService to create a service scope for command module registration. --- src/.DS_Store | Bin 12292 -> 14340 bytes src/Managing.Api/Program.cs | 45 ++++++++++-------- .../Discord/DiscordService.cs | 8 +++- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/.DS_Store b/src/.DS_Store index 59dd6be18dc67c6a6e9aac518ce8e889682bf57f..720babe60c2706722bd50fb48e72ed91fdb7515e 100644 GIT binary patch delta 173 zcmZokXem%&U|?W$DortDU@!nOIe-{M3-ADmb_NCoo{0+jjHVLOI`8F#saxpSWZ{|_B$2NJIy5nRw0r82;H74^4Y}>3Q`H&4L&&DXp o3N#G}47h=WE3)aE1sUHnPv+P0=5P7GyY5C`!lF6WzUBq6^!Kwu*uqvU2Dg?ns_ zUShD?SchExVU21g*ffWd(); -SentrySdk.Init(options => +// Initialize Sentry conditionally based on DSN availability +var sentryDsn = builder.Configuration["Sentry:Dsn"]; +if (!string.IsNullOrWhiteSpace(sentryDsn)) { - // A Sentry Data Source Name (DSN) is required. - // See https://docs.sentry.io/concepts/key-terms/dsn-explainer/ - // You can set it in the SENTRY_DSN environment variable, or you can set it in code here. - options.Dsn = builder.Configuration["Sentry:Dsn"]; + SentrySdk.Init(options => + { + // A Sentry Data Source Name (DSN) is required. + // See https://docs.sentry.io/concepts/key-terms/dsn-explainer/ + // You can set it in the SENTRY_DSN environment variable, or you can set it in code here. + options.Dsn = sentryDsn; - // When debug is enabled, the Sentry client will emit detailed debugging information to the console. - // This might be helpful, or might interfere with the normal operation of your application. - // We enable it here for demonstration purposes when first trying Sentry. - // You shouldn't do this in your applications unless you're troubleshooting issues with Sentry. - options.Debug = false; + // When debug is enabled, the Sentry client will emit detailed debugging information to the console. + // This might be helpful, or might interfere with the normal operation of your application. + // We enable it here for demonstration purposes when first trying Sentry. + // You shouldn't do this in your applications unless you're troubleshooting issues with Sentry. + options.Debug = false; - // Adds request URL and headers, IP and name for users, etc. - options.SendDefaultPii = true; + // Adds request URL and headers, IP and name for users, etc. + options.SendDefaultPii = true; - // This option is recommended. It enables Sentry's "Release Health" feature. - options.AutoSessionTracking = true; + // This option is recommended. It enables Sentry's "Release Health" feature. + options.AutoSessionTracking = true; - // Enabling this option is recommended for client applications only. It ensures all threads use the same global scope. - options.IsGlobalModeEnabled = false; + // Enabling this option is recommended for client applications only. It ensures all threads use the same global scope. + options.IsGlobalModeEnabled = false; - // Example sample rate for your transactions: captures 10% of transactions - options.TracesSampleRate = 0.1; + // Example sample rate for your transactions: captures 10% of transactions + options.TracesSampleRate = 0.1; - options.Environment = builder.Environment.EnvironmentName; -}); + options.Environment = builder.Environment.EnvironmentName; + }); +} builder.Services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy(), ["api"]); diff --git a/src/Managing.Infrastructure.Messengers/Discord/DiscordService.cs b/src/Managing.Infrastructure.Messengers/Discord/DiscordService.cs index f49277fa..a917bcab 100644 --- a/src/Managing.Infrastructure.Messengers/Discord/DiscordService.cs +++ b/src/Managing.Infrastructure.Messengers/Discord/DiscordService.cs @@ -1,4 +1,4 @@ -using Discord; +using Discord; using Discord.Commands; using Discord.Net; using Discord.WebSocket; @@ -64,7 +64,11 @@ namespace Managing.Infrastructure.Messengers.Discord _client.Log += Log; _commandService.Log += Log; // look for classes implementing ModuleBase to load commands from - await _commandService.AddModulesAsync(GetType().Assembly, _services); + // Create a service scope to resolve scoped services during command module registration + using (var scope = _services.CreateScope()) + { + await _commandService.AddModulesAsync(GetType().Assembly, scope.ServiceProvider); + } // log in to Discord, using the provided token await _client.LoginAsync(TokenType.Bot, _settings.Token); // start bot