dotnet/reactive · error · ArgumentNullException

subjectSelector

Error message

subjectSelector

What it means

The three-parameter Multicast overload throws ArgumentNullException with param name "subjectSelector" when the Func<ISubject<TSource, TIntermediate>> delegate is null. The subjectSelector is invoked once per connection to create the hub subject; without it the operator cannot build the multicast pipeline, so it is validated at call time.

Solutions

  1. Pass a non-null factory such as () => new Subject<TIntermediate>() (or AsyncSubject/BehaviorSubject as needed).
  2. If no custom subject is needed, use the simpler Publish/Select overload.
  3. Initialize the delegate field at declaration to avoid null in unassigned paths.

Example fix

// before
var result = source.Multicast(subjectFactory, xs => xs.Count()); // subjectFactory is null
// after
var result = source.Multicast(() => new Subject<int>(), xs => xs.Count());
Defensive patterns

Strategy: validation

Validate before calling

if (subjectSelector is null) throw new ArgumentNullException(nameof(subjectSelector));
var result = source.Multicast(subjectSelector, selector);

Type guard

static bool HasSubjectFactory<TSource, TInter>(Func<ISubject<TSource, TInter>>? f) => f is not null;

Try / catch

try { result = source.Multicast(subjectSelector, selector); }
catch (ArgumentNullException ex) when (ex.ParamName == "subjectSelector") { result = source.Multicast(() => new Subject<TSource>(), selector); }

Prevention

When it happens

Trigger: Passing null as the subject factory: source.Multicast(null, selector), or a variable holding a subject-creating lambda that was never assigned / resolved as null from configuration or DI.

Common situations: Abstracting subject creation behind a delegate (e.g. for testing) where the delegate defaults to null; conditional lambda assignment forgotten on some code path.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/9b9e8c72a55be681. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs:62

        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <typeparam name="TIntermediate">The type of the elements produced by the intermediate subject.</typeparam>
        /// <typeparam name="TResult">The type of the elements in the result sequence.</typeparam>
        /// <param name="source">Source sequence which will be multicasted in the specified selector function.</param>
        /// <param name="subjectSelector">Factory function to create an intermediate subject through which the source sequence's elements will be multicast to the selector function.</param>
        /// <param name="selector">Selector function which can use the multicasted source sequence subject to the policies enforced by the created subject.</param>
        /// <returns>An observable sequence that contains the elements of a sequence produced by multicasting the source sequence within a selector function.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="subjectSelector"/> or <paramref name="selector"/> is null.</exception>
        public static IObservable<TResult> Multicast<TSource, TIntermediate, TResult>(this IObservable<TSource> source, Func<ISubject<TSource, TIntermediate>> subjectSelector, Func<IObservable<TIntermediate>, IObservable<TResult>> selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (subjectSelector == null)
            {
                throw new ArgumentNullException(nameof(subjectSelector));
            }

            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            return s_impl.Multicast(source, subjectSelector, selector);
        }

        #endregion

        #region + Publish +

        /// <summary>
        /// Returns a connectable observable sequence that shares a single subscription to the underlying sequence.
        /// This operator is a specialization of Multicast using a regular <see cref="Subject{T}"/>.
        /// </summary>

View on GitHub (pinned to 94b5d5ab91)