From 7b17799b013fd8bc76a51b60d670a83901607e86 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Wed, 11 Jan 2023 22:07:41 -0500 Subject: [PATCH 1/4] Migrate from IWebHost to IHost --- Jellyfin.Server/Program.cs | 37 ++++++++----------- Jellyfin.Server/Startup.cs | 16 +++----- .../JellyfinApplicationFactory.cs | 2 +- 3 files changed, 22 insertions(+), 33 deletions(-) diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 7052f4d2bf..4725005ae6 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -6,7 +6,6 @@ using System.IO; using System.Linq; using System.Net; using System.Reflection; -using System.Runtime.InteropServices; using System.Runtime.Versioning; using System.Text; using System.Threading; @@ -21,7 +20,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -186,20 +184,26 @@ namespace Jellyfin.Server try { - var serviceCollection = new ServiceCollection(); - appHost.Init(serviceCollection); + var host = Host.CreateDefaultBuilder() + .ConfigureServices(services => + { + // NOTE: Called first to ensure app host configuration is fully initialized + appHost.Init(services); + }) + .ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.ConfigureWebHostBuilder(appHost, startupConfig, appPaths)) + .ConfigureAppConfiguration(config => config.ConfigureAppConfiguration(options, appPaths, startupConfig)) + .UseSerilog() + .Build(); - var webHost = new WebHostBuilder().ConfigureWebHostBuilder(appHost, serviceCollection, options, startupConfig, appPaths).Build(); - - // Re-use the web host service provider in the app host since ASP.NET doesn't allow a custom service collection. - appHost.ServiceProvider = webHost.Services; + // Re-use the host service provider in the app host since ASP.NET doesn't allow a custom service collection. + appHost.ServiceProvider = host.Services; await appHost.InitializeServices().ConfigureAwait(false); Migrations.MigrationRunner.Run(appHost, _loggerFactory); try { - await webHost.StartAsync(_tokenSource.Token).ConfigureAwait(false); + await host.StartAsync(_tokenSource.Token).ConfigureAwait(false); if (!OperatingSystem.IsWindows() && startupConfig.UseUnixSocket()) { @@ -284,16 +288,12 @@ namespace Jellyfin.Server /// /// The builder to configure. /// The application host. - /// The application service collection. - /// The command line options passed to the application. /// The application configuration. /// The application paths. /// The configured web host builder. public static IWebHostBuilder ConfigureWebHostBuilder( this IWebHostBuilder builder, - ApplicationHost appHost, - IServiceCollection serviceCollection, - StartupOptions commandLineOpts, + CoreAppHost appHost, IConfiguration startupConfig, IApplicationPaths appPaths) { @@ -349,14 +349,7 @@ namespace Jellyfin.Server _logger.LogInformation("Kestrel listening to unix socket {SocketPath}", socketPath); } }) - .ConfigureAppConfiguration(config => config.ConfigureAppConfiguration(commandLineOpts, appPaths, startupConfig)) - .UseSerilog() - .ConfigureServices(services => - { - // Merge the external ServiceCollection into ASP.NET DI - services.Add(serviceCollection); - }) - .UseStartup(); + .UseStartup(_ => new Startup(appHost)); } /// diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index 5d6a278c40..5996b3e305 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -35,20 +35,17 @@ namespace Jellyfin.Server /// public class Startup { - private readonly IServerConfigurationManager _serverConfigurationManager; private readonly IServerApplicationHost _serverApplicationHost; + private readonly IServerConfigurationManager _serverConfigurationManager; /// /// Initializes a new instance of the class. /// - /// The server configuration manager. - /// The server application host. - public Startup( - IServerConfigurationManager serverConfigurationManager, - IServerApplicationHost serverApplicationHost) + /// The server application host. + public Startup(CoreAppHost appHost) { - _serverConfigurationManager = serverConfigurationManager; - _serverApplicationHost = serverApplicationHost; + _serverApplicationHost = appHost; + _serverConfigurationManager = appHost.ConfigurationManager; } /// @@ -87,8 +84,7 @@ namespace Jellyfin.Server RequestHeaderEncodingSelector = (_, _) => Encoding.UTF8 }; - services - .AddHttpClient(NamedClient.Default, c => + services.AddHttpClient(NamedClient.Default, c => { c.DefaultRequestHeaders.UserAgent.Add(productHeader); c.DefaultRequestHeaders.Accept.Add(acceptJsonHeader); diff --git a/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs b/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs index c38faeda17..41b2273d04 100644 --- a/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs +++ b/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs @@ -82,7 +82,7 @@ namespace Jellyfin.Server.Integration.Tests appHost.Init(serviceCollection); // Configure the web host builder - Program.ConfigureWebHostBuilder(builder, appHost, serviceCollection, commandLineOpts, startupConfig, appPaths); + Program.ConfigureWebHostBuilder(builder, appHost, startupConfig, appPaths); } /// From 159e74ea09b623ab422c7000cb3621abd8de1118 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Wed, 11 Jan 2023 22:08:04 -0500 Subject: [PATCH 2/4] Update Serilog.AspNetCore --- Jellyfin.Server/Jellyfin.Server.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Server/Jellyfin.Server.csproj b/Jellyfin.Server/Jellyfin.Server.csproj index 195d7f3a98..b16c566d5b 100644 --- a/Jellyfin.Server/Jellyfin.Server.csproj +++ b/Jellyfin.Server/Jellyfin.Server.csproj @@ -41,7 +41,7 @@ - + From 0f46eca6a4e9f4b400c7a08defd033ce834e2974 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Wed, 11 Jan 2023 22:09:14 -0500 Subject: [PATCH 3/4] Minor cleanup in Startup class --- Jellyfin.Server/Startup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index 5996b3e305..f89f81c766 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -204,7 +204,7 @@ namespace Jellyfin.Server endpoints.MapControllers(); if (_serverConfigurationManager.Configuration.EnableMetrics) { - endpoints.MapMetrics("/metrics"); + endpoints.MapMetrics(); } endpoints.MapHealthChecks("/health"); From 033ffa9a8899be7f28e72603cf5881c066b40cd5 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Thu, 12 Jan 2023 11:51:12 -0500 Subject: [PATCH 4/4] Fix tests --- Jellyfin.Server/Program.cs | 6 +----- .../JellyfinApplicationFactory.cs | 15 +++++++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 4725005ae6..1506530f0c 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -185,11 +185,7 @@ namespace Jellyfin.Server try { var host = Host.CreateDefaultBuilder() - .ConfigureServices(services => - { - // NOTE: Called first to ensure app host configuration is fully initialized - appHost.Init(services); - }) + .ConfigureServices(services => appHost.Init(services)) .ConfigureWebHostDefaults(webHostBuilder => webHostBuilder.ConfigureWebHostBuilder(appHost, startupConfig, appPaths)) .ConfigureAppConfiguration(config => config.ConfigureAppConfiguration(options, appPaths, startupConfig)) .UseSerilog() diff --git a/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs b/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs index 41b2273d04..1bfa5996d8 100644 --- a/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs +++ b/tests/Jellyfin.Server.Integration.Tests/JellyfinApplicationFactory.cs @@ -8,6 +8,7 @@ using MediaBrowser.Common; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; @@ -78,11 +79,17 @@ namespace Jellyfin.Server.Integration.Tests commandLineOpts, startupConfig); _disposableComponents.Add(appHost); - var serviceCollection = new ServiceCollection(); - appHost.Init(serviceCollection); - // Configure the web host builder - Program.ConfigureWebHostBuilder(builder, appHost, startupConfig, appPaths); + builder.ConfigureServices(services => appHost.Init(services)) + .ConfigureWebHostBuilder(appHost, startupConfig, appPaths) + .ConfigureAppConfiguration((context, builder) => + { + builder + .SetBasePath(appPaths.ConfigurationDirectoryPath) + .AddInMemoryCollection(ConfigurationOptions.DefaultConfiguration) + .AddEnvironmentVariables("JELLYFIN_") + .AddInMemoryCollection(commandLineOpts.ConvertToConfig()); + }); } ///