microsoft/aspire · error · InvalidOperationException
. needs to be set when a custom Execution Strategy is…
Error message
{nameof(NpgsqlEntityFrameworkCorePostgreSQLSettings)}.{nameof(NpgsqlEntityFrameworkCorePostgreSQLSettings.DisableRetry)} needs to be set when a custom Execution Strategy is configured. What it means
EnrichNpgsqlDbContext detects a custom ExecutionStrategy registered on the DbContextOptions and throws because automatic retry cannot be combined with a custom strategy — the developer must explicitly set DisableRetry to acknowledge retrying is disabled.
Solutions
- Set DisableRetry = true in NpgsqlEntityFrameworkCorePostgreSQLSettings (builder.EnrichNpgsqlDbContext<TContext>(settings => settings.DisableRetry = true))
- Remove the custom execution strategy so EF Core uses the default NpgsqlExecutionStrategy and let Aspire manage retries
- Register the custom strategy only when explicitly disabling Aspire retry
Example fix
// before builder.EnrichNpgsqlDbContext<MyDbContext>(); // DbContext uses custom execution strategy // after builder.EnrichNpgsqlDbContext<MyDbContext>(settings => settings.DisableRetry = true);
Defensive patterns
Strategy: validation
Validate before calling
bool hasCustomStrategy = myDbContextOptions.ExecutionStrategy is not null &&
myDbContextOptions.ExecutionStrategy.GetType() != typeof(NpgsqlExecutionStrategy);
if (hasCustomStrategy)
aspireSettings.DisableRetry = true; // set before EnrichNpgsqlDbContext Try / catch
try
{
builder.EnrichNpgsqlDbContext<MyDbContext>();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("DisableRetry"))
{
logger.LogError(ex, "Custom execution strategy conflicts with Aspire retry settings.");
throw;
} Prevention
- Decide up front whether Aspire or the DbContext owns retry policy
- Set DisableRetry whenever the DbContext configures its own execution strategy
- Avoid subclassing NpgsqlExecutionStrategy when using Aspire enrichment
When it happens
Trigger: Calling EnrichNpgsqlDbContext on a DbContext whose options already have an execution strategy whose exact type is not NpgsqlExecutionStrategy (e.g. from UseNpgsql with EnableRetryOnFailure customization or a custom ExecutionStrategy impl), while NpgsqlEntityFrameworkCorePostgreSQLSettings.DisableRetry is false/not set.
Common situations: Migrating an existing DbContext that configured its own retrying strategy into Aspire's enrichment pipeline; subclassing NpgsqlExecutionStrategy for custom logic; mixing UseNpgsql(...) options with Aspire enrichment.
Understand the failure class
Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.
Related errors
- . needs to be set when a custom Execution Strategy is…
- A database name is required but was not provided. Specify…
- A DbContext could not be configured. Ensure valid…
- A DbContextOptions< > was not found. Please ensure…
- Conflicting values for 'CommandTimeout' were found in
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/79b3fb09769c6f53.
Report an issue: GitHub.
Appendix: source
Thrown at src/Components/Aspire.Npgsql.EntityFrameworkCore.PostgreSQL/AspireEFPostgreSqlExtensions.cs:161
var extension = optionsBuilder.Options.FindExtension<NpgsqlOptionsExtension>();
if (!settings.DisableRetry)
{
var executionStrategy = extension?.ExecutionStrategyFactory?.Invoke(new ExecutionStrategyDependencies(null!, optionsBuilder.Options, null!));
if (executionStrategy != null)
{
if (executionStrategy is NpgsqlRetryingExecutionStrategy)
{
// Keep custom Retry strategy.
// Any sub-class of NpgsqlRetryingExecutionStrategy is a valid retry strategy
// which shouldn't be replaced even with DisableRetry == false
}
else if (executionStrategy.GetType() != typeof(NpgsqlExecutionStrategy))
{
// Check NpgsqlExecutionStrategy specifically (no 'is'), any sub-class is treated as a custom strategy.
throw new InvalidOperationException($"{nameof(NpgsqlEntityFrameworkCorePostgreSQLSettings)}.{nameof(NpgsqlEntityFrameworkCorePostgreSQLSettings.DisableRetry)} needs to be set when a custom Execution Strategy is configured.");
}
else
{
options.EnableRetryOnFailure();
}
}
else
{
options.EnableRetryOnFailure();
}
}
if (settings.CommandTimeout.HasValue)
{
if (extension != null &&
extension.CommandTimeout.HasValue &&
extension.CommandTimeout != settings.CommandTimeout)
{View on GitHub (pinned to 25830f84bd)