{"record":{"id":"c6ca508edff43813","repo":"dotnet/reactive","slug":"strings-linq-no-elements-firstasync","errorCode":null,"errorMessage":"Strings_Linq.NO_ELEMENTS","messagePattern":"Strings_Linq\\.NO_ELEMENTS","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/FirstAsync.cs","lineNumber":44,"sourceCode":"                public _(IObserver<TSource> observer)\n                    : base(observer)\n                {\n                }\n\n                public override void OnNext(TSource value)\n                {\n                    _found = true;\n                    ForwardOnNext(value);\n                    ForwardOnCompleted();\n                }\n\n                public override void OnCompleted()\n                {\n                    if (!_found)\n                    {\n                        try\n                        {\n                            throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);\n                        }\n                        catch (Exception e)\n                        {\n                            ForwardOnError(e);\n                        }\n                    }\n                }\n            }\n        }\n\n        internal sealed class Predicate : Producer<TSource, Predicate._>\n        {\n            private readonly IObservable<TSource> _source;\n            private readonly Func<TSource, bool> _predicate;\n\n            public Predicate(IObservable<TSource> source, Func<TSource, bool> predicate)\n            {\n                _source = source;","sourceCodeStart":26,"sourceCodeEnd":62,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/FirstAsync.cs#L26-L62","documentation":"FirstAsync without a predicate throws InvalidOperationException with Strings_Linq.NO_ELEMENTS when the source completes without emitting any value, because there is no first element. Thrown in OnCompleted when _found is still false and forwarded via OnError.","triggerScenarios":"Subscribing to source.FirstAsync() where the source completes with zero OnNext calls.","commonSituations":"Taking the first message from an empty queue stream; awaiting the first event of a source that has already completed; filtering upstream left nothing to observe.","solutions":["Use FirstAsync().ToList then FirstOrDefault semantics, or FirstOrDefault via .FirstOrDefault() on the observable for a default value instead of an error","Catch InvalidOperationException in OnError and supply a default","Ensure the source actually emits before completion"],"exampleFix":"// before\nsource.FirstAsync().Subscribe(x => ..., ex => throw ex);\n// after\nsource.FirstOrDefaultAsync().Subscribe(x => Console.WriteLine(x));","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"source.FirstAsync().Subscribe(\n    x => Console.WriteLine(x),\n    ex => { if (ex is InvalidOperationException) Console.WriteLine(default); else throw ex; });","preventionTips":["Use FirstOrDefaultAsync when emptiness is possible","Handle OnError on every FirstAsync subscription","Confirm sources are not already-completed hot observables"],"tags":["reactive","linq","first","empty-sequence"],"backgroundTag":"empty-result-set","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"}