diff --git a/SPTarkov.Server/Program.cs b/SPTarkov.Server/Program.cs index fba3dc97a..1b424cef3 100644 --- a/SPTarkov.Server/Program.cs +++ b/SPTarkov.Server/Program.cs @@ -64,7 +64,6 @@ public static async Task Main(string[] args) builder.Services.AddSingleton(builder); builder.Services.AddSingleton>(loadedMods); - builder.Services.AddHostedService(); // Configure Kestrel options ConfigureKestrel(builder); @@ -74,9 +73,9 @@ public static async Task Main(string[] args) ConfigureWebApp(app); // In case of exceptions we snatch a Server logger - var serverExceptionLogger = app.Services.GetService()!.CreateLogger("Server"); + var serverExceptionLogger = app.Services.GetRequiredService().CreateLogger("Server"); // We need any logger instance to use as a finalizer when the app closes - var loggerFinalizer = app.Services.GetService>()!; + var loggerFinalizer = app.Services.GetRequiredService>(); try { // Handle edge cases where reverse proxies might pass X-Forwarded-For, use this as the actual IP address @@ -86,6 +85,8 @@ public static async Task Main(string[] args) SetConsoleOutputMode(); + await app.Services.GetRequiredService().Startup(); + await app.RunAsync(); } catch (Exception ex) @@ -111,7 +112,7 @@ private static void ConfigureWebApp(WebApplication app) app.Use( async (HttpContext context, RequestDelegate _) => { - await context.RequestServices.GetService()!.HandleRequest(context); + await context.RequestServices.GetRequiredService().HandleRequest(context); } ); } @@ -122,8 +123,8 @@ private static void ConfigureKestrel(WebApplicationBuilder builder) (_, options) => { // This method is not expected to be async so we need to wait for the Task instead of using await keyword - options.ApplicationServices.GetService()!.OnLoad().Wait(); - var httpConfig = options.ApplicationServices.GetService()?.GetConfig()!; + options.ApplicationServices.GetRequiredService().OnLoad().Wait(); + var httpConfig = options.ApplicationServices.GetRequiredService().GetConfig(); // Probe the http ip and port to see if its being used, this method will throw an exception and crash // the server if the IP/Port combination is already in use @@ -138,7 +139,7 @@ private static void ConfigureKestrel(WebApplicationBuilder builder) listener?.Stop(); } - var certHelper = options.ApplicationServices.GetService()!; + var certHelper = options.ApplicationServices.GetRequiredService(); options.Listen( IPAddress.Parse(httpConfig.Ip), httpConfig.Port, diff --git a/SPTarkov.Server/Services/SptServerBackgroundService.cs b/SPTarkov.Server/Services/SptServerStartupService.cs similarity index 78% rename from SPTarkov.Server/Services/SptServerBackgroundService.cs rename to SPTarkov.Server/Services/SptServerStartupService.cs index 1195c4c8b..3d0e7741a 100644 --- a/SPTarkov.Server/Services/SptServerBackgroundService.cs +++ b/SPTarkov.Server/Services/SptServerStartupService.cs @@ -1,13 +1,15 @@ using System.Runtime; +using SPTarkov.DI.Annotations; using SPTarkov.Server.Core.Loaders; using SPTarkov.Server.Core.Models.Spt.Mod; using SPTarkov.Server.Core.Utils; namespace SPTarkov.Server.Services; -public class SptServerBackgroundService(IReadOnlyList loadedMods, BundleLoader bundleLoader, App app) : BackgroundService +[Injectable(InjectionType.Singleton)] +public class SptServerStartupService(IReadOnlyList loadedMods, BundleLoader bundleLoader, App app) { - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + public async Task Startup() { if (ProgramStatics.MODS()) {