{"record":{"id":"1ba7f32511de1fee","repo":"dotnet/reactive","slug":"minobservers","errorCode":null,"errorMessage":"minObservers","messagePattern":"minObservers","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs","lineNumber":311,"sourceCode":"\n        /// <summary>\n        /// Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Connectable observable sequence.</param>\n        /// <param name=\"minObservers\">The minimum number of observers required to subscribe before establishing the connection to the source.</param>\n        /// <returns>An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"minObservers\"/> is non-positive.</exception>\n        public static IObservable<TSource> RefCount<TSource>(this IConnectableObservable<TSource> source, int minObservers)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n            if (minObservers <= 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(minObservers));\n            }\n\n            return s_impl.RefCount(source, minObservers);\n        }\n\n        /// <summary>\n        /// Returns an observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Connectable observable sequence.</param>\n        /// <param name=\"minObservers\">The minimum number of observers required to subscribe before establishing the connection to the source.</param>\n        /// <param name=\"disconnectDelay\">The time span that should be waited before possibly unsubscribing from the connectable observable.</param>\n        /// <returns>An observable sequence that stays connected to the source as long as there is at least one subscription to the observable sequence.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"minObservers\"/> is non-positive.</exception>\n        public static IObservable<TSource> RefCount<TSource>(this IConnectableObservable<TSource> source, int minObservers, TimeSpan disconnectDelay)\n        {\n            if (source == null)","sourceCodeStart":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs#L293-L329","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nvar shared = connectable.RefCount(minObserversFromConfig); // 0 when unset\n// after\nvar shared = connectable.RefCount(Math.Max(1, minObserversFromConfig));","handlingStrategy":"validation","validationCode":"if (minObservers < 1) throw new ArgumentOutOfRangeException(nameof(minObservers), minObservers, \"Must be >= 1\");\nvar shared = connectable.RefCount(minObservers);","typeGuard":"static bool IsValidMinObservers(int n) => n >= 1;","tryCatchPattern":"try { var shared = connectable.RefCount(minObservers); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"minObservers\")\n{\n    var shared = connectable.RefCount(1); // safe default\n}","preventionTips":["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."],"tags":["dotnet","system-reactive","argument-out-of-range","argument-validation"],"backgroundTag":"argument-out-of-range","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}