{"record":{"id":"2ed7d3f923757434","repo":"dotnet/reactive","slug":"source-dispatcherobservable","errorCode":null,"errorMessage":"source","messagePattern":"source","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs","lineNumber":30,"sourceCode":"    /// Provides a set of extension methods for scheduling actions performed through observable sequences on UI dispatchers.\n    /// </summary>\n    public static class DispatcherObservable\n    {\n        #region ObserveOn[Dispatcher]\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"dispatcher\">Dispatcher whose associated message loop is used to notify observers on.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>\n        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"dispatcher\"/> is null.</exception>\n        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, Dispatcher dispatcher)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (dispatcher == null)\n            {\n                throw new ArgumentNullException(nameof(dispatcher));\n            }\n\n            return ObserveOn_(source, dispatcher);\n        }\n\n        /// <summary>\n        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.\n        /// </summary>\n        /// <typeparam name=\"TSource\">The type of the elements in the source sequence.</typeparam>\n        /// <param name=\"source\">Source sequence.</param>\n        /// <param name=\"dispatcher\">Dispatcher whose associated message loop is used to notify observers on.</param>\n        /// <param name=\"priority\">Priority to schedule work items at.</param>\n        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Linq/DispatcherObservable.cs#L12-L48","documentation":"DispatcherObservable.ObserveOn (WPF) marshals observer callbacks onto the given WPF Dispatcher. It throws ArgumentNullException with parameter name 'source' when the observable sequence is null; a null sequence cannot be wrapped or subscribed.","triggerScenarios":"Calling nullSource.ObserveOn(dispatcher) — e.g. a service or repository method returned null instead of an IObservable, or an observable field was never assigned before composition.","commonSituations":"MVVM WPF apps where a ViewModel's data stream is null until initialization completes; DI container returning null for an unregistered binding; legacy code migrated from events where streams are optional.","solutions":["Ensure the source IObservable<TSource> is non-null before calling ObserveOn.","Make producers return Observable.Empty<T>() or Observable.Throw instead of null.","Guard the composition with a null check or null-coalescing fallback."],"exampleFix":"// before\n_items = _repository.StreamItems(); // may be null\n_items.ObserveOn(Dispatcher).Subscribe(Add);\n// after\n_items = _repository.StreamItems() ?? Observable.Empty<Item>();\n_items.ObserveOn(Dispatcher).Subscribe(Add);","handlingStrategy":"validation","validationCode":"if (source == null) source = Observable.Empty<TSource>();\nif (dispatcher == null) throw new InvalidOperationException(\"Dispatcher unavailable\");\nsource.ObserveOn(dispatcher).Subscribe(observer);","typeGuard":"static bool CanObserveOn<TSource>(IObservable<TSource> source, Dispatcher dispatcher) => source is not null && dispatcher is not null;","tryCatchPattern":"try { source.ObserveOn(dispatcher).Subscribe(OnNext); }\ncatch (ArgumentNullException ex) when (ex.ParamName is \"source\" or \"dispatcher\") { log.Error($\"ObserveOn got null {ex.ParamName}\"); }","preventionTips":["Use nullable reference types so null observable flows fail at compile time","Register observables in DI so they are never resolved as null","Initialize ViewModel streams in constructors, not on first access"],"tags":["argument-null","wpf","rxjs","dispatcher"],"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"}