Skip to content

Instantly share code, notes, and snippets.

@karenpayneoregon
Created February 1, 2025 12:09
Show Gist options
  • Save karenpayneoregon/18f253833ea3befac39057ec99a61994 to your computer and use it in GitHub Desktop.
Save karenpayneoregon/18f253833ea3befac39057ec99a61994 to your computer and use it in GitHub Desktop.
Example for checking if sections exists in appsettings.json
{
"ConnectionStrings": {
"MainConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=NorthWind;Integrated Security=True;Encrypt=False",
"SecondaryConnection": "..."
},
"EntityConfiguration": {
"CreateNew": true
}
}
using System.Text.Json;
namespace SomeLibrary.Classes;
/// <summary>
/// Provides utility methods for validating the presence of specific sections
/// in the "appsettings.json" configuration file.
/// </summary>
/// <remarks>
/// This class is designed to assist in ensuring that critical configuration
/// sections, such as "EntityConfiguration" and "ConnectionStrings", are
/// present in the application's configuration file. These checks are
/// essential for maintaining the integrity of application setup and
/// preventing runtime errors due to missing configurations.
/// </remarks>
public class JsonHelpers
{
private static string FileName => "appsettings.json";
/// <summary>
/// Determines whether the "EntityConfiguration" section exists in the "appsettings.json" file.
/// </summary>
/// <returns>
/// <c>true</c> if the "EntityConfiguration" section exists; otherwise, <c>false</c>.
/// </returns>
public static bool EntityConfigurationSectionExists()
{
string jsonContent = File.ReadAllText(FileName);
using JsonDocument doc = JsonDocument.Parse(jsonContent);
return doc.RootElement.TryGetProperty("EntityConfiguration", out _);
}
/// <summary>
/// Determines whether the "ConnectionStrings" section exists in the "appsettings.json" file.
/// </summary>
/// <returns>
/// <c>true</c> if the "ConnectionStrings" section exists; otherwise, <c>false</c>.
/// </returns>
public static bool ConnectionStringsSectionExists()
{
string jsonContent = File.ReadAllText(FileName);
using JsonDocument doc = JsonDocument.Parse(jsonContent);
return doc.RootElement.TryGetProperty("ConnectionStrings", out _);
}
/// <summary>
/// Determines whether the "MainConnection" entry exists within the "ConnectionStrings"
/// section of the "appsettings.json" file.
/// </summary>
/// <returns>
/// <c>true</c> if the "MainConnection" entry exists within the "ConnectionStrings" section;
/// otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method is useful for verifying the presence of a specific database connection
/// configuration, ensuring that the required "MainConnection" entry is available before
/// proceeding with application logic.
/// </remarks>
public static bool MainConnectionExists()
{
string jsonContent = File.ReadAllText(FileName);
using JsonDocument doc = JsonDocument.Parse(jsonContent);
return doc.RootElement.TryGetProperty("ConnectionStrings", out JsonElement connectionStrings) &&
connectionStrings.TryGetProperty("MainConnection", out _);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment