microsoft/aspire · error · ArgumentOutOfRangeException

Must be in the range

Error message

Must be in the range: [{min}{minMessage}, {max}{maxMessage}]

What it means

Guard.Range (private helper behind the in-range checks) validates that a value lies within [min, max] and throws ArgumentOutOfRangeException with 'Must be in the range: [{min}{minMessage}, {max}{maxMessage}]'. The optional min/max messages annotate whether bounds are exclusive (e.g. '(0', '0)').

Solutions

  1. Read the range from the message and clamp/normalize the value into it before the call.
  2. Validate the value at configuration-load time.
  3. Fix arithmetic that pushed the value out of range.
  4. If the caller's range expectation is wrong, adjust the min/max arguments at the check site.

Example fix

// before
Guard.ThrowIfNotInRange(percent, 0, 100); // percent == 150
// after
percent = Math.Clamp(percent, 0, 100);
Guard.ThrowIfNotInRange(percent, 0, 100);
Defensive patterns

Strategy: validation

Validate before calling

if (value < min || value > max) throw new ArgumentOutOfRangeException(nameof(value), value, $"must be in [{min},{max}]");

Try / catch

catch (ArgumentOutOfRangeException ex) { /* clamp ActualValue into the stated range */ }

Prevention

When it happens

Trigger: Calling any Guard in-range check with a value outside the specified min/max bounds.

Common situations: Config values outside supported bounds (timeout too large, percentage > 100); off-by-one when passing an exclusive bound; unbounded counters growing past a limit.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at src/Vendoring/OpenTelemetry.Instrumentation.StackExchangeRedis/Shared/Guard.cs:229

        }

        [DebuggerHidden]
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        private static void Range<T>(T value, string? paramName, T min, T max, string? minName, string? maxName, string? message)
            where T : IComparable<T>
        {
            if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)
            {
                var minMessage = minName != null ? $": {minName}" : string.Empty;
                var maxMessage = maxName != null ? $": {maxName}" : string.Empty;
                var exMessage = message ?? string.Format(
                    CultureInfo.InvariantCulture,
                    "Must be in the range: [{0}{1}, {2}{3}]",
                    min,
                    minMessage,
                    max,
                    maxMessage);
                throw new ArgumentOutOfRangeException(paramName, value, exMessage);
            }
        }
    }
}

View on GitHub (pinned to 25830f84bd)