diff --git a/src/Titano.Server/Hosting/ServerHost.cs b/src/Titano.Server/Hosting/ServerHost.cs index cda06dfa..2f4cf173 100644 --- a/src/Titano.Server/Hosting/ServerHost.cs +++ b/src/Titano.Server/Hosting/ServerHost.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.AspNetCore.Server.Kestrel.Core; using Titano.Core.Diagnostics; using Titano.Data; @@ -15,9 +16,11 @@ namespace Titano.Server.Hosting; /// /// Builds the Titano Server web application from (used by Program, the Windows service -/// host and tests). The archive module plugs in through of : -/// it must register and , and may add -/// s; the server refuses to start without the first two. +/// host and tests). The archive module plugs in through the configureServices callback of , +/// which runs before the server's own registrations: it must register and +/// (the server refuses to start without them), may add +/// s, and may resolve , +/// and from DI in its factories. /// public static class ServerHost { @@ -38,6 +41,10 @@ public static class ServerHost builder.Logging.AddFilter("Microsoft.AspNetCore", Microsoft.Extensions.Logging.LogLevel.Warning); builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", Microsoft.Extensions.Logging.LogLevel.Information); + // The archive module registers first, so a missing piece stops the server before it touches the database. + configureServices?.Invoke(builder.Services); + RequireArchiveModule(builder.Services); + using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true)); var bootLogger = bootLoggerFactory.CreateLogger("Titano.Server"); var certificate = CertificateStore.Load(options, bootLogger); @@ -66,10 +73,8 @@ public static class ServerHost }); }); ConfigureServices(builder.Services, options, state, database, archive); - configureServices?.Invoke(builder.Services); var app = builder.Build(); - RequireArchiveModule(app.Services); Log.SetSink(new LoggerSink(app.Services.GetRequiredService())); ConfigurePipeline(app, options); app.Logger.LogInformation("Titano Server {Version} · archive {Archive} · data {Data} · {Database} · TLS {Tls}{Fingerprint}", @@ -93,7 +98,7 @@ public static class ServerHost services.AddSingleton(state); services.AddSingleton(database); services.AddSingleton(archive); - services.AddSingleton(TimeProvider.System); + services.TryAddSingleton(TimeProvider.System); services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default)); services.Configure(o => o.ThrowOnBadRequest = true); services.Configure(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30)); @@ -152,12 +157,11 @@ public static class ServerHost } /// Fails fast when the archive module's services are missing (the server would accept uploads it cannot commit). - private static void RequireArchiveModule(IServiceProvider services) + private static void RequireArchiveModule(IServiceCollection services) { - var registry = services.GetRequiredService(); var missing = new List(); - if (!registry.IsService(typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)"); - if (!registry.IsService(typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)"); + if (!services.Any(d => d.ServiceType == typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)"); + if (!services.Any(d => d.ServiceType == typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)"); if (missing.Count > 0) { throw new InvalidOperationException(