dotnet/reactive · error · ArgumentOutOfRangeException
minObservers
Error message
minObservers
What it means
RefCount(source, minObservers) throws ArgumentOutOfRangeException when minObservers is zero or negative. At least one observer must be required; the operator uses this number to decide when to connect and disconnect the underlying connectable observable.
Solutions
- Pass a value >= 1 for minObservers; clamp with Math.Max(1, n) if the value is computed.
- Fix the config/source producing 0 so it supplies a sensible minimum (typically 1).
- Drop the minObservers parameter and use plain RefCount(source), which behaves like a minimum of 1.
- Validate the setting at application startup with a clear error message.
Example fix
// before var shared = connectable.RefCount(minObserversFromConfig); // 0 when unset // after var shared = connectable.RefCount(Math.Max(1, minObserversFromConfig));
Defensive patterns
Strategy: validation
Validate before calling
if (minObservers < 1) throw new ArgumentOutOfRangeException(nameof(minObservers), minObservers, "Must be >= 1"); var shared = connectable.RefCount(minObservers);
Type guard
static bool IsValidMinObservers(int n) => n >= 1;
Try / catch
try { var shared = connectable.RefCount(minObservers); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "minObservers")
{
var shared = connectable.RefCount(1); // safe default
} Prevention
- Clamp computed thresholds with Math.Max(1, n).
- Validate observer-count config at startup (must be >= 1).
- Use plain RefCount(source) when the minimum is 1.
- Don't use default(int) as a placeholder for minObservers.
When it happens
Trigger: Calling Observable.RefCount(connectable, 0) or a negative count, usually from a computed value such as an empty list's Count, a config value of 0, or default(int).
Common situations: Config file missing a minSubscribers setting defaulting to 0, Math.Min over an empty collection, or a wrong overload chosen where the author meant to pass nothing.
Related errors
- new ArgumentOutOfRangeException(nameof(capacity))
- ArgumentOutOfRangeException: capacity
- Specified argument was out of the range of valid values…
- throw new ArgumentOutOfRangeException(nameof(bufferSize));
- throw new ArgumentOutOfRangeException(nameof(retryCount));
AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15).
Data as JSON: /api/errors/1ba7f32511de1fee.
Report an issue: GitHub.
Appendix: source
Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs:311
/// <summary>
/// Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Connectable observable sequence.</param>
/// <param name="minObservers">The minimum number of observers required to subscribe before establishing the connection to the source.</param>
/// <returns>An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="minObservers"/> is non-positive.</exception>
public static IObservable<TSource> RefCount<TSource>(this IConnectableObservable<TSource> source, int minObservers)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
if (minObservers <= 0)
{
throw new ArgumentOutOfRangeException(nameof(minObservers));
}
return s_impl.RefCount(source, minObservers);
}
/// <summary>
/// Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.
/// </summary>
/// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
/// <param name="source">Connectable observable sequence.</param>
/// <param name="minObservers">The minimum number of observers required to subscribe before establishing the connection to the source.</param>
/// <param name="disconnectDelay">The time span that should be waited before possibly unsubscribing from the connectable observable.</param>
/// <returns>An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="minObservers"/> is non-positive.</exception>
public static IObservable<TSource> RefCount<TSource>(this IConnectableObservable<TSource> source, int minObservers, TimeSpan disconnectDelay)
{
if (source == null)View on GitHub (pinned to 94b5d5ab91)