{"record":{"id":"32771bfc523210e0","repo":"dotnet/reactive","slug":"more-than-one-element","errorCode":null,"errorMessage":"MORE_THAN_ONE_ELEMENT","messagePattern":"MORE_THAN_ONE_ELEMENT","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable/SingleAsync.cs","lineNumber":38,"sourceCode":"            protected override void Run(_ sink) => sink.Run(_source);\n\n            internal sealed class _ : IdentitySink<TSource>\n            {\n                private TSource? _value;\n                private bool _seenValue;\n\n                public _(IObserver<TSource> observer)\n                    : base(observer)\n                {\n                }\n\n                public override void OnNext(TSource value)\n                {\n                    if (_seenValue)\n                    {\n                        try\n                        {\n                            throw new InvalidOperationException(Strings_Linq.MORE_THAN_ONE_ELEMENT);\n                        }\n                        catch (Exception e)\n                        {\n                            ForwardOnError(e);\n                        }\n                        return;\n                    }\n\n                    _value = value;\n                    _seenValue = true;\n                }\n\n                public override void OnCompleted()\n                {\n                    if (!_seenValue)\n                    {\n                        try\n                        {","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable/SingleAsync.cs#L20-L56","documentation":"SingleAsync requires the source to emit exactly one element. On a second OnNext while a value was already seen, the operator throws InvalidOperationException(Strings_Linq.MORE_THAN_ONE_ELEMENT) and forwards it via OnError. This mirrors LINQ's Single() semantics for sequences with more than one element.","triggerScenarios":"source.SingleAsync() subscribed to a stream that emits two or more OnNext values before completing.","commonSituations":"Assuming an event fires once (e.g. 'configuration loaded') but it fires on reloads; hot observables or subjects receiving multiple pushes; debounce/missing distinct logic letting duplicates through.","solutions":["Ensure the stream emits exactly one value (use Take(1), FirstAsync, or .DistinctUntilChanged() if duplicates are identical).","If multiple values are legitimate, use FirstAsync/LastAsync instead of SingleAsync.","Handle the InvalidOperationException via OnError and fall back to First/Last semantics."],"exampleFix":"// before\nvar value = await configLoaded.SingleAsync();\n// after\nvar value = await configLoaded.Take(1).FirstAsync();","handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { var v = await source.SingleAsync(); }\ncatch (InvalidOperationException) { var v = await source.FirstAsync(); }","preventionTips":["Use FirstAsync or Take(1) when multiple emissions are possible.","Add DistinctUntilChanged when duplicates should be collapsed.","Only use SingleAsync when exactly-one is a real invariant."],"tags":["linq","single-operator","multiple-elements","invalid-operation"],"backgroundTag":"value-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"}