Successfully added
ASP.NET Core
by Patrik
How to Stop Azure Errors by Validating Settings at Startup
The easiest and safest fix is to validate configuration values before Azure services are registered. This prevents accidental fallback authentication and gives clear feedback if something is missing.
Here’s a clean version of the solution:
public static IServiceCollection AddAzureResourceGraphClient(
this IServiceCollection services,
IConfiguration config)
{
var connectionString = config["Authentication:AzureServiceAuthConnectionString"];
if (string.IsNullOrWhiteSpace(connectionString))
throw new InvalidOperationException(
"Missing 'Authentication:AzureServiceAuthConnectionString' configuration."
);
services.AddSingleton(_ => new AzureServiceTokenProvider(connectionString));
return services;
}
This small addition gives you:
✔ Clear error messages
✔ Consistent behavior between environments
✔ No more unexpected Azure calls during tests
✔ Easier debugging for teammates
For larger apps, you can also use strongly typed configuration + validation (IOptions<T>), which helps keep settings organized and ensures nothing slips through the cracks.
With this guard in place, your integration tests stay clean, predictable, and Azure-free unless you want them to involve Azure.
bestpractices
azure
di
startup
testing
Referenced in:
Comments