From efef1165d6c8ca949b2d8c63ca28caeb40ebce43 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Mon, 30 Mar 2026 09:51:56 +0100 Subject: [PATCH 1/3] refactor CreateHostBuilder --- EstateReportingAPI/Program.cs | 122 ++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 58 deletions(-) diff --git a/EstateReportingAPI/Program.cs b/EstateReportingAPI/Program.cs index d92c7e4..2ed3343 100644 --- a/EstateReportingAPI/Program.cs +++ b/EstateReportingAPI/Program.cs @@ -23,13 +23,22 @@ public static IHostBuilder CreateHostBuilder(string[] args) //At this stage, we only need our hosting file for ip and ports - FileInfo fi = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location); + FileInfo fi = new(Assembly.GetExecutingAssembly().Location); - IConfigurationRoot config = new ConfigurationBuilder().SetBasePath(fi.Directory.FullName) - .AddJsonFile("hosting.json", optional: true) - .AddJsonFile("hosting.development.json", optional: true) - .AddEnvironmentVariables().Build(); + ConfigureLogging(); + IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args); + hostBuilder.UseWindowsService(); + hostBuilder.UseLamar(); + hostBuilder.ConfigureLogging(logging => + { + logging.AddConsole(); + logging.AddNLog(); + }); + return ConfigureWebHost(hostBuilder, fi.Directory.FullName); + } + + private static void ConfigureLogging() { String contentRoot = Directory.GetCurrentDirectory(); String nlogConfigPath = Path.Combine(contentRoot, "nlog.config"); @@ -43,61 +52,58 @@ public static IHostBuilder CreateHostBuilder(string[] args) }); b.LoadConfigurationFromFile(nlogConfigPath); }); + } - IHostBuilder hostBuilder = Host.CreateDefaultBuilder(args); - hostBuilder.UseWindowsService(); - hostBuilder.UseLamar(); - hostBuilder.ConfigureLogging(logging => - { - logging.AddConsole(); - logging.AddNLog(); - }); + private static IHostBuilder ConfigureWebHost(IHostBuilder hostBuilder, String basePath) { + IConfigurationRoot config; hostBuilder.ConfigureWebHostDefaults(webBuilder => - { - webBuilder.ConfigureAppConfiguration((context, configBuilder) => - { - var env = context.HostingEnvironment; - - configBuilder.SetBasePath(fi.Directory.FullName) - .AddJsonFile("hosting.json", optional: true) - .AddJsonFile($"hosting.{env.EnvironmentName}.json", optional: true) - .AddJsonFile("/home/txnproc/config/appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"/home/txnproc/config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) - .AddEnvironmentVariables(); - - // Build a snapshot of configuration so we can use it immediately (e.g. for Sentry) - var builtConfig = configBuilder.Build(); - - // Keep existing static usage (if you must), and initialise the ConfigurationReader now. - Startup.Configuration = builtConfig; - ConfigurationReader.Initialise(Startup.Configuration); - - // Configure Sentry on the webBuilder using the config snapshot. - var sentrySection = builtConfig.GetSection("SentryConfiguration"); - if (sentrySection.Exists()) - { - // Replace the condition below if you intended to only enable Sentry in certain environments. - if (env.IsDevelopment() == false) - { - webBuilder.UseSentry(o => - { - o.Dsn = builtConfig["SentryConfiguration:Dsn"]; - o.SendDefaultPii = true; - o.MaxRequestBodySize = RequestSize.Always; - o.CaptureBlockingCalls = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "CaptureBlockingCalls", false); - o.IncludeActivityData = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "IncludeActivityData", false); - o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; - }); - } - } - }); - - webBuilder.UseStartup(); - webBuilder.UseConfiguration(config); - webBuilder.UseKestrel(); - }); + { + webBuilder.ConfigureAppConfiguration((context, configBuilder) => + { + IWebHostEnvironment env = context.HostingEnvironment; + + configBuilder.SetBasePath(basePath) + .AddJsonFile("hosting.json", optional: true) + .AddJsonFile($"hosting.{env.EnvironmentName}.json", optional: true) + .AddJsonFile("/home/txnproc/config/appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"/home/txnproc/config/appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) + .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true) + .AddEnvironmentVariables(); + + // Keep existing static usage (if you must), and initialise the ConfigurationReader now. + Startup.Configuration = configBuilder.Build(); ; + ConfigurationReader.Initialise(Startup.Configuration); + + ConfigureSentry(env, webBuilder); + }); + + webBuilder.UseStartup(); + webBuilder.UseConfiguration(Startup.Configuration); + webBuilder.UseKestrel(); + }); return hostBuilder; } + + private static void ConfigureSentry(IWebHostEnvironment env, + IWebHostBuilder webBuilder) { + // Configure Sentry on the webBuilder using the config snapshot. + IConfigurationSection sentrySection = Startup.Configuration.GetSection("SentryConfiguration"); + if (sentrySection.Exists()) + { + // Replace the condition below if you intended to only enable Sentry in certain environments. + if (env.IsDevelopment() == false) + { + webBuilder.UseSentry(o => + { + o.Dsn = Startup.Configuration["SentryConfiguration:Dsn"]; + o.SendDefaultPii = true; + o.MaxRequestBodySize = RequestSize.Always; + o.CaptureBlockingCalls = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "CaptureBlockingCalls", false); + o.IncludeActivityData = ConfigurationReader.GetValueOrDefault("SentryConfiguration", "IncludeActivityData", false); + o.Release = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "unknown"; + }); + } + } + } } \ No newline at end of file From 09e2c45ae59365f0f71b005b6b9fd40c2525b5ed Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Mon, 30 Mar 2026 10:09:13 +0100 Subject: [PATCH 2/3] Remove unused IConfigurationRoot variable --- EstateReportingAPI/Program.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/EstateReportingAPI/Program.cs b/EstateReportingAPI/Program.cs index 2ed3343..b8d52e2 100644 --- a/EstateReportingAPI/Program.cs +++ b/EstateReportingAPI/Program.cs @@ -55,7 +55,6 @@ private static void ConfigureLogging() { } private static IHostBuilder ConfigureWebHost(IHostBuilder hostBuilder, String basePath) { - IConfigurationRoot config; hostBuilder.ConfigureWebHostDefaults(webBuilder => { webBuilder.ConfigureAppConfiguration((context, configBuilder) => @@ -72,7 +71,7 @@ private static IHostBuilder ConfigureWebHost(IHostBuilder hostBuilder, String ba .AddEnvironmentVariables(); // Keep existing static usage (if you must), and initialise the ConfigurationReader now. - Startup.Configuration = configBuilder.Build(); ; + Startup.Configuration = configBuilder.Build(); ConfigurationReader.Initialise(Startup.Configuration); ConfigureSentry(env, webBuilder); @@ -106,4 +105,4 @@ private static void ConfigureSentry(IWebHostEnvironment env, } } } -} \ No newline at end of file +} From 88a84dac8b6e0b1d6904e9712618e2ead5a82188 Mon Sep 17 00:00:00 2001 From: Stuart Ferguson Date: Mon, 30 Mar 2026 11:52:59 +0100 Subject: [PATCH 3/3] oops --- EstateReportingAPI/Program.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/EstateReportingAPI/Program.cs b/EstateReportingAPI/Program.cs index b8d52e2..1a2a2f5 100644 --- a/EstateReportingAPI/Program.cs +++ b/EstateReportingAPI/Program.cs @@ -78,7 +78,6 @@ private static IHostBuilder ConfigureWebHost(IHostBuilder hostBuilder, String ba }); webBuilder.UseStartup(); - webBuilder.UseConfiguration(Startup.Configuration); webBuilder.UseKestrel(); }); return hostBuilder;