I have an application that is going to use some external microservices I've built. The application and the microservices are hosted on Azure App Services. The microservices' functionality is needed inside the Startup
class, and so I'm registering those dependencies early (while creating the IWebHostBuilder
). The microservices also depend on configuration values (supplied by the application), so I'm also doing some IOptions
Pattern configuration while creating the IWebHostBuilder
to get settings from appsettings.json
. That settings file contains settings that will be used in all environment, as well as a few that are environment-specific. It was my intention to use the settings from the file for local development, and to override the environment-specific settings using the Configuration blade on the App Service. In other applications I've built, the Configuration blade settings have indeed overridden the values from appsettings.json
.
However, it appears that when you do an early configuration, as in the below code, the App Service fails to override the settings, and so my local development settings end up being used in the App Service instead (and so everything blows up). I took a shot in the dark and moved the environment-specific settings out of appsettings.json
and into their own JSON file with a different name. Now I'm only loading that file early. This did nothing to solve my problem.
So my question is, what techniques are there for having configuration settings available before Startup
, but still being able to override those settings in Azure App Services? I've scoured the .NET Core documentation on configuration, but there are so many options and approaches that they make my head spin. I'm currently using .NET Core 2.2. Thanks in advance!
internal static class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.Build()
.Run();
}
private static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
return WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, builder) => { builder.AddEarlyRequiredConfiguration(); })
.ConfigureServices((context, services) => { services.AddEarlyRequiredServices(context); })
.UseStartup<Startup>();
}
private static void AddEarlyRequiredServices(this IServiceCollection services, WebHostBuilderContext context)
{
// This extension method is in a separate assembly, and registers the microservices.
// It also configures the IOptions stuff for the configuration values.
services.AddMyMicroservices(context);
}
private static void AddEarlyRequiredConfiguration(this IConfigurationBuilder builder)
{
builder.AddJsonFile("appsettings.json");
}
}