{"record":{"id":"5ec2e59ba052f70b","repo":"dotnet/reactive","slug":"value-cannot-be-null-parameter-source-publishlast","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/PublishLast.cs","lineNumber":17,"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.Subjects;\nusing System.Threading.Tasks;\n\nnamespace System.Reactive.Linq\n{\n    // REVIEW: Expose PublishLast using ConcurrentAsyncAsyncSubject<T> underneath.\n\n    public partial class AsyncObservable\n    {\n        public static IConnectableAsyncObservable<TSource> PublishLast<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Multicast(source, new SequentialAsyncAsyncSubject<TSource>());\n        }\n\n        public static IAsyncObservable<TResult> PublishLast<TSource, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncObservable<TSource>, IAsyncObservable<TResult>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (selector == null)\n                throw new ArgumentNullException(nameof(selector));\n\n            return Multicast(source, () => new SequentialAsyncAsyncSubject<TSource>(), selector);\n        }\n\n        public static IAsyncObservable<TResult> PublishLast<TSource, TResult>(this IAsyncObservable<TSource> source, Func<IAsyncObservable<TSource>, ValueTask<IAsyncObservable<TResult>>> selector)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/PublishLast.cs#L1-L35","documentation":"System.ArgumentNullException thrown by PublishLast<TSource>(source) in PublishLast.cs:17, with Parameter 'source'. PublishLast multicasts only the final element via SequentialAsyncAsyncSubject and, like Publish, eagerly rejects a null IAsyncObservable<TSource> before Multicast is invoked.","triggerScenarios":"Calling source.PublishLast() where source is null, e.g. an IAsyncObservable<T> variable that was never assigned or a producer that returned null.","commonSituations":"Aggregating 'last value' semantics over a stream whose source comes from an async service that returned null; test scaffolding without a source; pipeline composition where an earlier operator returned null instead of throwing.","solutions":["Ensure a valid non-null observable is passed to PublishLast; trace the null-producing assignment.","Substitute AsyncObservable.Empty<TSource>() when a missing source is an expected condition.","Check the code that builds the source (factory, await result, field init) and add your own null guard with a meaningful message."],"exampleFix":"// before\nIAsyncObservable<int> source = null;\nvar last = source.PublishLast();\n\n// after\nvar source = await GetSourceAsync() ?? AsyncObservable.Empty<int>();\nvar last = source.PublishLast();","handlingStrategy":"validation","validationCode":"if (source is null) throw new InvalidOperationException(\"Cannot PublishLast: source observable is null\");\nvar last = source.PublishLast();","typeGuard":"static bool HasSource<T>(IAsyncObservable<T>? s) => s is not null;","tryCatchPattern":"try { var last = source.PublishLast(); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"source\") { /* fallback: AsyncObservable.Empty<TSource>().PublishLast() or rethrow with context */ }","preventionTips":["Verify the source producer returns an observable, never null.","Coalesce missing sources with AsyncObservable.Empty<T>().","Enable nullable reference types to surface null flows during compilation."],"tags":["argumentnull","null-check","reactive-extensions","publishlast-operator"],"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"}