Managing Configuration Settings in .NET Applications: Using Inheritance with appsettings.json in Monorepo

Maroun Maroun
Level Up Coding
Published in
3 min readFeb 1, 2023

--

Maintaining settings for each subproject within a monorepo can be difficult. An effective solution is to use a parent settings file that is shared among all subprojects. This can help to prevent duplication of configuration and allow for easy management of common settings across all subprojects.

Photo by Klara Kulikova on Unsplash

In .NET Core, you can use the built-in configuration system to manage your application settings. This configuration allows you to create a parent appsettings file that contains shared settings between all subprojects. You can then create specific appsettings files for each subproject that inherit from the parent file.

To achieve this, you can create a parent appsettings.json file in the root of the monorepo and add it to the configuration using the AddJsonFile method. For example, you can create a file called parent_appsettings.json with the following contents:

{
"ConnectionStrings": {
"DefaultConnection": "<your connection>"
},
"Datadog": {
"URL": "<your url>"
}
}

You can then add this file to the configuration in each subproject’s Program.cs file. Let’s create a common loader method that can be used by each subproject who needs the parent settings:

/// CommonLoader.cs

public static void LoadParentAppsettings(WebApplicationBuilder builder, IConfiguration configuration)
{
builder.Configuration
.SetBasePath(Directory.GetParent(builder.Environment.ContentRootPath).FullName)
.AddJsonFile(configuration.GetValue<string>("ParentAppsettings"),
optional: false,
reloadOnChange: true);
}

Using ParentAppSettings key allow you to have different parents for different environments (i.e. local, staging, and production). For example, in a specific subproject, you can include a “ParentAppSettings” property that points to the parent file. For example:

// Production
{
"ParentAppsettings": "parent_appsettings.json",
"SubProjectSettings": {
"ApiUrl": "https://localhost:8001"
}
}
// Staging
{
"ParentAppsettings": "parent_appsettings.Staging.json",
"SubProjectSettings": {
"ApiUrl": "https://localhost:8003"
}
}

Note: the parent appsettings files will overwrite the specific settings files.

Managing environment-specific settings

In addition to sharing common settings across all subprojects, it’s also important to consider how to manage environment-specific settings. For example, you may have different database connection strings for your development, staging, and production environments.

One way to manage environment-specific settings is by using a different appsettings file for each environment. For example, you can create an appsettings.Development.json file, an appsettings.Staging.json file, and an appsettings.json file. Each of these files can inherit from the parent file and override any settings that are specific to that environment.

Another way is using the ASPNETCORE_ENVIRONMENT environment variable to determine which environment the application is running in and load the appropriate appsettings file:

Configuration = new ConfigurationBuilder()
.AddJsonFile("parent_appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"parent_appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true)
...

The above code first loads the parent appsettings file, then it loads an environment-specific appsettings file and overrides the previous one.

Conclusion

Managing settings for multiple projects within a monorepo can be challenging, but using shared appsettings parent inheritance in .NET Core applications can make this process easier and more efficient. By defining common settings in a parent file and inheriting from that file in subproject-specific files, you can avoid duplication of configuration and easily manage settings across all subprojects. Additionally, by managing environment-specific settings, you can ensure that your application behaves correctly in different environments.

--

--