From 753c702cccc2c5dc01c0bf91a4bb24e716fa887d Mon Sep 17 00:00:00 2001 From: Justin Baur <19896123+justindbaur@users.noreply.github.com> Date: Wed, 14 Jan 2026 13:32:52 -0500 Subject: [PATCH] Update to `IHostBuilder` style --- test/Server.IntegrationTest/Server.cs | 29 ++++++++----------- util/Server/Program.cs | 40 +++++++++++++-------------- 2 files changed, 30 insertions(+), 39 deletions(-) diff --git a/test/Server.IntegrationTest/Server.cs b/test/Server.IntegrationTest/Server.cs index 073dbffb5a..963e6e0ecd 100644 --- a/test/Server.IntegrationTest/Server.cs +++ b/test/Server.IntegrationTest/Server.cs @@ -1,6 +1,6 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; -using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.Configuration; namespace Bit.Server.IntegrationTest; @@ -12,34 +12,27 @@ public class Server : WebApplicationFactory public bool? WebVault { get; set; } public string? AppIdLocation { get; set; } - protected override IWebHostBuilder? CreateWebHostBuilder() + protected override void ConfigureWebHost(IWebHostBuilder builder) { - var args = new List + base.ConfigureWebHost(builder); + + var config = new Dictionary { - "/contentRoot", - ContentRoot ?? "", - "/webRoot", - WebRoot ?? "", - "/serveUnknown", - ServeUnknown.ToString().ToLowerInvariant(), + {"contentRoot", ContentRoot}, + {"webRoot", WebRoot}, + {"serveUnknown", ServeUnknown.ToString().ToLowerInvariant()}, }; if (WebVault.HasValue) { - args.Add("/webVault"); - args.Add(WebVault.Value.ToString().ToLowerInvariant()); + config["webVault"] = WebVault.Value.ToString().ToLowerInvariant(); } if (!string.IsNullOrEmpty(AppIdLocation)) { - args.Add("/appIdLocation"); - args.Add(AppIdLocation); + config["appIdLocation"] = AppIdLocation; } - var builder = WebHostBuilderFactory.CreateFromTypesAssemblyEntryPoint([.. args]) - ?? throw new InvalidProgramException("Could not create builder from assembly."); - - builder.UseSetting("TEST_CONTENTROOT_SERVER", ContentRoot); - return builder; + builder.UseConfiguration(new ConfigurationBuilder().AddInMemoryCollection(config).Build()); } } diff --git a/util/Server/Program.cs b/util/Server/Program.cs index 3d563830ab..edebb7e0e3 100644 --- a/util/Server/Program.cs +++ b/util/Server/Program.cs @@ -6,27 +6,30 @@ namespace Bit.Server; public class Program { public static void Main(string[] args) - { - var builder = CreateWebHostBuilder(args); - var host = builder.Build(); - host.Run(); - } - - public static IWebHostBuilder CreateWebHostBuilder(string[] args) { var config = new ConfigurationBuilder() .AddCommandLine(args) .Build(); - var builder = new WebHostBuilder() - .UseConfiguration(config) - .UseKestrel() - .UseStartup() - .ConfigureLogging((hostingContext, logging) => + var builder = new HostBuilder() + .ConfigureWebHost(builder => { - logging.AddConsole().AddDebug(); + builder.UseConfiguration(config); + builder.UseKestrel(); + builder.UseStartup(); + builder.ConfigureKestrel((_,_) => {}); + + var webRoot = config.GetValue("webRoot"); + if (string.IsNullOrWhiteSpace(webRoot)) + { + builder.UseWebRoot(webRoot); + } }) - .ConfigureKestrel((context, options) => { }); + .ConfigureLogging(logging => + { + logging.AddConsole() + .AddDebug(); + }); var contentRoot = config.GetValue("contentRoot"); if (!string.IsNullOrWhiteSpace(contentRoot)) @@ -38,12 +41,7 @@ public class Program builder.UseContentRoot(Directory.GetCurrentDirectory()); } - var webRoot = config.GetValue("webRoot"); - if (string.IsNullOrWhiteSpace(webRoot)) - { - builder.UseWebRoot(webRoot); - } - - return builder; + var host = builder.Build(); + host.Run(); } }