Sunday, 20 April 2025

Advanced Configuration in .NET Core

Leave a Comment

 

1. Configuration Providers

.NET Core supports many configuration sources.


  • appsettings.json
  • appsettings.{Environment}.json
  • Environment variables
  • Command-line arguments
  • User Secrets
  • Azure Key Vault
var builder = WebApplication.CreateBuilder(args);

builder.Configuration
    .AddJsonFile("appsettings.json")
    .AddJsonFile(
        $"appsettings.{builder.Environment.EnvironmentName}.json",
        optional: true
    )
    .AddEnvironmentVariables()
    .AddCommandLine(args);
2. Strongly Typed Configuration

Map configuration to C# classes using the IOptions pattern.

// appsettings.json
{
  "MySettings": {
    "SiteTitle": "My App",
    "MaxItems": 10
  }
}
public class MySettings
{
    public string SiteTitle { get; set; }
    public int MaxItems { get; set; }
}
builder.Services.Configure<MySettings>(
    builder.Configuration.GetSection("MySettings")
);
public class HomeController : Controller
{
    private readonly MySettings _settings;
    public HomeController(IOptions<MySettings> options)
    {
        _settings = options.Value;
    }
}
3. Validate Options at Startup
builder.Services
    .AddOptions<MySettings>()
    .Bind(builder.Configuration.GetSection("MySettings"))
    .Validate(settings => settings.MaxItems > 0, "MaxItems must be > 0");
4. Reloading Config on Changes

Enable hot reload for appsettings.json.

builder.Configuration
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

Use IOptionsSnapshot<T> to get updated config per request.

5. Secret Management

Use User Secrets for local development.

dotnet user-secrets init
dotnet user-secrets set "MySettings:ApiKey" "123456"
builder.Configuration
       .AddUserSecrets<Program>();
6. Azure Key Vault Integration
builder.Configuration.AddAzureKeyVault(
    new Uri("https://yourvault.vault.azure.net/"),
    new DefaultAzureCredential());

Make sure your app is registered and granted access to the vault.

7. Custom Configuration Provider

You can create a custom provider for things like databases or external services.

  • Inherit from ConfigurationProvider and ConfigurationSource
  • Inject it into the builder with the Add method

Points to consider

  • Use IConfiguration for raw access to values
  • Use IOptions for dependency injection & structured data
  • Store secrets outside of source-controlled files
  • Group related settings into sections

ASP.NET Core 9.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 9.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment