{"record":{"id":"e0c600cb9baf952d","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-refcount","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'source')","messagePattern":"Value cannot be null\\. \\(Parameter 'source'\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/RefCount.cs","lineNumber":16,"sourceCode":"﻿// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT License.\n// See the LICENSE file in the project root for more information. \n\nusing System.Reactive.Disposables;\nusing System.Reactive.Subjects;\nusing System.Threading;\n\nnamespace System.Reactive.Linq\n{\n    public partial class AsyncObservable\n    {\n        public static IAsyncObservable<TSource> RefCount<TSource>(this IConnectableAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            var gate = new AsyncGate();\n            var count = 0;\n            var connectable = default(IAsyncDisposable);\n\n            return Create<TSource>(async observer =>\n            {\n                var subscription = await source.SubscribeSafeAsync(observer).ConfigureAwait(false);\n\n                using (await gate.LockAsync().ConfigureAwait(false))\n                {\n                    if (++count == 1)\n                    {\n                        connectable = await source.ConnectAsync().ConfigureAwait(false);\n                    }\n                }\n\n                return AsyncDisposable.Create(async () =>","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/RefCount.cs#L1-L34","documentation":"RefCount wraps an IConnectableAsyncObservable so it connects when the first observer subscribes and disconnects when the last unsubscribes. This guard clause throws ArgumentNullException immediately when the source connectable observable is null, because RefCount cannot operate without a source to attach to.","triggerScenarios":"Calling AsyncObservable.Recount source) or any method chain whose upstream operator returned null (e.g. a factory method or cached variable that was never assigned) instead of a real IConnectableAsyncObservable<TSource>.","commonSituations":"A Publish/RefCount chain where Publish was called on a variable that is null; DI or configuration produced no connectable source; refactoring removed an assignment so the connectable flows as null.","solutions":["Check the value passed to RefCount is a non-null IConnectableAsyncObservable<TSource> before calling it","Trace the producer of the source (e.g. Publish/AsyncPublish call) and fix why it returned null","Guard the call site with a null check or throw a descriptive exception upstream","Verify the source variable is initialized before the subscription chain is built"],"exampleFix":"// before\nIConnectableAsyncObservable<int> conn = maybePublish();\nvar obs = conn.RefCount(); // throws if maybePublish() returned null\n// after\nIConnectableAsyncObservable<int> conn = maybePublish() ?? throw new InvalidOperationException(\"Publish returned null source\");\nvar obs = conn.RefCount();","handlingStrategy":"validation","validationCode":"if (source == null) throw new InvalidOperationException(\"RefCount requires a non-null IConnectableAsyncObservable; check the upstream Publish call.\");\nvar refCounted = AsyncObservable.RefCount(source);","typeGuard":"static bool IsConnectable<TSource>(object o) => o is IConnectableAsyncObservable<TSource> conn && conn != null;","tryCatchPattern":"try\n{\n    var refCounted = AsyncObservable.RefCount(connectable);\n}\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\")\n{\n    // log and fall back to a default connectable source\n    connectable = upstream.AsyncPublish();\n    var refCounted = AsyncObservable.RefCount(connectable);\n}","preventionTips":["Never build RefCount chains from nullable variables; assert non-null right after Publish","Enable nullable reference types (C# 8+) so null flow is caught at compile time","Keep Publish/RefCount pairs in the same method scope to avoid null handoff","Unit-test subscription chains with construction-time argument validation"],"tags":["csharp","null-check","argument-validation","reactive"],"backgroundTag":"null-argument","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"}