Nov 04 2024
{
"Config": {
"Database": "mssqlConnectionString",
"Redis": "redisConnectionString",
"RedisPassword": "someDummyPassword"
}
}
var builder = WebApplication.CreateBuilder(args);
// Load configuration from appsettings.json
builder.Configuration.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
// Register services
builder.Services.AddControllers();
// Access configuration settings and register as services if needed
string databaseConnectionString = builder.Configuration["Config:Database"];
string redisConnectionString = builder.Configuration["Config:Redis"];
string redisPassword = builder.Configuration["Config:RedisPassword"];
string environment = Environment.GetEnvironmentVariable("ENVIRONMENT");
string jsonFile = $"appsettings.{environment}.json";
builder.Configuration
.AddJsonFile("appsettings.json", optional: false,reloadOnChange: true)
.AddJsonFile(jsonFile, optional: true);
string? keyVaultUrl = builder.Configuration["KeyVault"];
var credentials = new DefaultAzureCredential ();
builder.Configuration.AddAzureKeyVault(new Uri(keyVaultUrl), credentials);
public class MyService
{
private readonly string _database;
private readonly string _redis;
public MyService(IConfiguration configuration)
{
// Retrieve secrets from Key Vault
_database = configuration["Config:Database"];
_redis = configuration["Config:Redis"];
}
// Use the secrets in your application logic
}
Join 13,250+ subscribers to improve your .NET Knowledge.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Subscribe to the TheCodeMan.net and be among the 13,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.