Advertisement

Configuration & Options Pattern interview questions and answers

15 Configuration & Options Pattern interview questions for ASP.NET Core roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Advanced Save

How would you write a custom configuration provider to load settings from an unsupported source, like a remote configuration service or database?

Implement IConfigurationSource (returning an IConfigurationProvider instance) and a corresponding ConfigurationProvider subclass overriding Load() to populate the provider's internal Data dictionary from your custom source, then register it via configurationBuilder.Add(new MyCustomSource()) -- integrating seamlessly with the rest of the configuration system, including layering with other providers and, if you implement change detection, triggering hot-reload.
public class DatabaseConfigurationProvider : ConfigurationProvider {
    public override void Load() {
        using var connection = new SqlConnection(_connectionString);
        var settings = connection.Query("SELECT [Key], [Value] FROM AppSettings");
        foreach (var s in settings) Data[s.Key] = s.Value;
    }
}

public class DatabaseConfigurationSource : IConfigurationSource {
    public IConfigurationProvider Build(IConfigurationBuilder builder) => new DatabaseConfigurationProvider();
}
Real-world example
A multi-tenant SaaS platform builds a custom configuration provider that loads tenant-specific feature flags from a database table at startup, integrating seamlessly alongside the standard appsettings.json and environment variable providers.
Likely follow-ups
  • How would you add live change detection (polling or push notifications) to this custom provider?
  • How does provider registration order affect precedence for a custom source?
Entity Framework Core & Data Access Multiple Inheritance & MRO
Q12 Intermediate Save

How do you use IHostEnvironment/IWebHostEnvironment to conditionally configure services based on the current environment?

IWebHostEnvironment exposes properties like EnvironmentName, IsDevelopment(), IsProduction(), and IsStaging(), letting startup code branch its configuration logic based on which environment the application is running in, resolved from the ASPNETCORE_ENVIRONMENT environment variable at host startup.
if (builder.Environment.IsDevelopment()) {
    builder.Services.AddSingleton<IEmailSender, FakeEmailSender>();  // no real emails sent locally
} else {
    builder.Services.AddSingleton<IEmailSender, SmtpEmailSender>();
}
Real-world example
An application registers a fake, no-op email sender when running locally in Development (avoiding accidental real emails during testing) but wires up the real SMTP implementation for Staging and Production, all controlled by the same environment detection.
Likely follow-ups
  • How is the environment name actually determined at startup?
  • How do you add support for a custom environment name beyond the standard three?
Testing ASP.NET Core Applications Hosting Models: Kestrel IIS & Reverse Proxies
Q13 Advanced Save

How would you design configuration for a multi-tenant ASP.NET Core application where each tenant needs different settings resolved at runtime?

Since the standard Options pattern's IOptionsSnapshot is scoped per-request but not inherently tenant-aware, a common approach implements a custom tenant-aware settings resolver service that layers tenant-specific database or cache overrides on top of a global default IOptionsMonitor<T> baseline, resolving the current tenant's specific configuration based on request context (subdomain, header, or claim) rather than relying on the built-in Options pattern alone.
public class TenantSettingsResolver(IOptionsMonitor<Dictionary<string, TenantSettings>> allSettings, ITenantContext tenantContext) {
    public TenantSettings GetCurrent() {
        return allSettings.CurrentValue.TryGetValue(tenantContext.TenantId, out var settings)
            ? settings
            : _defaultSettings;
    }
}
Real-world example
A B2B SaaS platform resolves each tenant's custom branding colors, feature flags, and rate limits through a tenant-aware settings resolver that layers tenant-specific database overrides on top of a global default configuration baseline.
Likely follow-ups
  • How do you cache tenant-specific settings efficiently without a database hit on every request?
  • How does this pattern interact with configuration hot-reload for tenant-specific changes?
Multiple Inheritance & MRO Caching
Q14 Intermediate Save

How does the command-line configuration provider let you override settings via arguments when running an application, and what's its precedence?

The command-line provider (registered by default) parses arguments in --Key=Value or --Key Value format (supporting colon-delimited nested keys) as the highest-precedence configuration source by default, letting you override any setting for a single run without modifying files or environment variables -- useful for quick local testing or scripted one-off overrides.
dotnet run --ConnectionStrings:DefaultConnection="Server=test;" --Logging:LogLevel:Default=Debug

// Equivalent to setting these keys via any other provider, but only for this single run
Real-world example
A developer debugging a production-like issue locally runs `dotnet run --Logging:LogLevel:Default=Trace` for one session to get maximally verbose logging without permanently changing any configuration file.
Likely follow-ups
  • Does the command-line provider support short-form argument aliases?
  • How does this interact with environment-variable overrides in terms of precedence?
.NET CLI SDK & Project Structure (csproj) Logging
Q15 Advanced Save

How do you write a custom IValidateOptions<T> implementation for validation logic beyond what DataAnnotations attributes can express?

IValidateOptions<T>.Validate(name, options) lets you implement arbitrary custom validation logic (like cross-field validation checking that one setting's value is consistent with another's, or an external check like verifying a configured URL is reachable) returning ValidateOptionsResult.Success or ValidateOptionsResult.Fail(errorMessage), registered via services.AddSingleton<IValidateOptions<T>, MyValidator>() and automatically invoked when combined with ValidateOnStart.
public class SmtpSettingsValidator : IValidateOptions<SmtpSettings> {
    public ValidateOptionsResult Validate(string? name, SmtpSettings options) {
        if (options.UseSsl && options.Port != 465 && options.Port != 587)
            return ValidateOptionsResult.Fail("SSL requires port 465 or 587");
        return ValidateOptionsResult.Success;
    }
}

builder.Services.AddSingleton<IValidateOptions<SmtpSettings>, SmtpSettingsValidator>();
Real-world example
A configuration validator catches an invalid combination (SSL enabled but using a non-SSL port) at application startup that simple per-field DataAnnotations attributes couldn't express, since it depends on the relationship between two separate settings.
Likely follow-ups
  • How does this combine with DataAnnotations validation running on the same options class?
  • What's the execution order if multiple IValidateOptions implementations exist for the same type?
Global Exception Handling & Middleware Diagnostics & Performance

Showing 11–15 of 15