Reordered services startup (#558)

* Reordered services startup

* Removed unnecessary comment

* Made startup service required

* Marked other services as required

* Removed unnecessary ! for null

---------

Co-authored-by: Alex <clodanSPT@hotmail.com>
Co-authored-by: Chomp <27521899+chompDev@users.noreply.github.com>
This commit is contained in:
clodanSPT
2025-08-19 10:32:54 +01:00
committed by GitHub
parent 7102711ef8
commit 22ab053cda
2 changed files with 12 additions and 9 deletions
+8 -7
View File
@@ -64,7 +64,6 @@ public static class Program
builder.Services.AddSingleton(builder); builder.Services.AddSingleton(builder);
builder.Services.AddSingleton<IReadOnlyList<SptMod>>(loadedMods); builder.Services.AddSingleton<IReadOnlyList<SptMod>>(loadedMods);
builder.Services.AddHostedService<SptServerBackgroundService>();
// Configure Kestrel options // Configure Kestrel options
ConfigureKestrel(builder); ConfigureKestrel(builder);
@@ -74,9 +73,9 @@ public static class Program
ConfigureWebApp(app); ConfigureWebApp(app);
// In case of exceptions we snatch a Server logger // In case of exceptions we snatch a Server logger
var serverExceptionLogger = app.Services.GetService<ILoggerFactory>()!.CreateLogger("Server"); var serverExceptionLogger = app.Services.GetRequiredService<ILoggerFactory>().CreateLogger("Server");
// We need any logger instance to use as a finalizer when the app closes // We need any logger instance to use as a finalizer when the app closes
var loggerFinalizer = app.Services.GetService<ISptLogger<App>>()!; var loggerFinalizer = app.Services.GetRequiredService<ISptLogger<App>>();
try try
{ {
// Handle edge cases where reverse proxies might pass X-Forwarded-For, use this as the actual IP address // Handle edge cases where reverse proxies might pass X-Forwarded-For, use this as the actual IP address
@@ -86,6 +85,8 @@ public static class Program
SetConsoleOutputMode(); SetConsoleOutputMode();
await app.Services.GetRequiredService<SptServerStartupService>().Startup();
await app.RunAsync(); await app.RunAsync();
} }
catch (Exception ex) catch (Exception ex)
@@ -111,7 +112,7 @@ public static class Program
app.Use( app.Use(
async (HttpContext context, RequestDelegate _) => async (HttpContext context, RequestDelegate _) =>
{ {
await context.RequestServices.GetService<HttpServer>()!.HandleRequest(context); await context.RequestServices.GetRequiredService<HttpServer>().HandleRequest(context);
} }
); );
} }
@@ -122,8 +123,8 @@ public static class Program
(_, options) => (_, options) =>
{ {
// This method is not expected to be async so we need to wait for the Task instead of using await keyword // This method is not expected to be async so we need to wait for the Task instead of using await keyword
options.ApplicationServices.GetService<OnWebAppBuildModLoader>()!.OnLoad().Wait(); options.ApplicationServices.GetRequiredService<OnWebAppBuildModLoader>().OnLoad().Wait();
var httpConfig = options.ApplicationServices.GetService<ConfigServer>()?.GetConfig<HttpConfig>()!; var httpConfig = options.ApplicationServices.GetRequiredService<ConfigServer>().GetConfig<HttpConfig>();
// Probe the http ip and port to see if its being used, this method will throw an exception and crash // 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 // the server if the IP/Port combination is already in use
@@ -138,7 +139,7 @@ public static class Program
listener?.Stop(); listener?.Stop();
} }
var certHelper = options.ApplicationServices.GetService<CertificateHelper>()!; var certHelper = options.ApplicationServices.GetRequiredService<CertificateHelper>();
options.Listen( options.Listen(
IPAddress.Parse(httpConfig.Ip), IPAddress.Parse(httpConfig.Ip),
httpConfig.Port, httpConfig.Port,
@@ -1,13 +1,15 @@
using System.Runtime; using System.Runtime;
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.Loaders; using SPTarkov.Server.Core.Loaders;
using SPTarkov.Server.Core.Models.Spt.Mod; using SPTarkov.Server.Core.Models.Spt.Mod;
using SPTarkov.Server.Core.Utils; using SPTarkov.Server.Core.Utils;
namespace SPTarkov.Server.Services; namespace SPTarkov.Server.Services;
public class SptServerBackgroundService(IReadOnlyList<SptMod> loadedMods, BundleLoader bundleLoader, App app) : BackgroundService [Injectable(InjectionType.Singleton)]
public class SptServerStartupService(IReadOnlyList<SptMod> loadedMods, BundleLoader bundleLoader, App app)
{ {
protected override async Task ExecuteAsync(CancellationToken stoppingToken) public async Task Startup()
{ {
if (ProgramStatics.MODS()) if (ProgramStatics.MODS())
{ {