microsoft/aspire · error · ArgumentOutOfRangeException

The value cannot be negative.

Error message

The value cannot be negative.

What it means

ValidateOptionalWholeSecondDuration rejects negative TimeSpan values passed as optional duration options (e.g. timeouts) for Azure sandbox publishing. Durations in the sandbox configuration must be zero or positive.

Solutions

  1. Set the duration to a non-negative TimeSpan value
  2. Clamp with TimeSpan.FromTicks(Math.Max(0, value.Ticks)) before passing options
  3. Fix the computation/config source that produced the negative value

Example fix

// before
var options = new AzureSandboxOptions { IdleTimeout = TimeSpan.FromSeconds(-10) };
// after
var options = new AzureSandboxOptions { IdleTimeout = TimeSpan.FromSeconds(10) };
Defensive patterns

Strategy: validation

Validate before calling

if (options.IdleTimeout is { } t && t < TimeSpan.Zero) throw new ArgumentException("Duration must be non-negative");

Prevention

When it happens

Trigger: PublishAsAzureSandbox (via ValidateSandboxOptions) given an options duration property set to TimeSpan.FromSeconds(-5) or TimeSpan.FromMinutes(-1), or the default TimeSpan value computed from a negative input.

Common situations: Computing a timeout as target time minus current time (which can go negative); reading a duration from config where a negative number was supplied; using TimeSpan default arithmetic that underflows.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/38d9f0a641bca47f. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Sandboxes/AzureSandboxesExtensions.cs:427

            }

            if (!names.Add(endpoint.Name))
            {
                throw new ArgumentException($"Endpoint option '{endpoint.Name}' is configured more than once.", nameof(options));
            }
        }
    }

    private static void ValidateOptionalWholeSecondDuration(TimeSpan? value, string paramName, TimeSpan? maximum = null)
    {
        if (value is null)
        {
            return;
        }

        if (value < TimeSpan.Zero)
        {
            throw new ArgumentOutOfRangeException(paramName, "The value cannot be negative.");
        }

        if (value.Value.Ticks % TimeSpan.TicksPerSecond != 0)
        {
            throw new ArgumentException("The value must use whole-second precision.", paramName);
        }

        if (maximum is not null && value > maximum)
        {
            throw new ArgumentOutOfRangeException(paramName, $"The value cannot exceed {maximum}.");
        }
    }

    private static void ValidateOptionalEnum<TEnum>(TEnum? value, string paramName)
        where TEnum : struct, Enum
    {
        if (value is not null && !Enum.IsDefined(value.Value))
        {

View on GitHub (pinned to 25830f84bd)