Debugging the Reactive Subscribe extension method

The short story:

In some of my ViewModels, I’m not able to subscribe to ReactiveUI Commands although all other Reactive-specific code works, while in others the .Subscribe(...) method works properly.

The long story:

The context: An MVVM project based on the ReactiveUI framework, with all ViewModels inheriting from ReactiveObject, all View classes implementing the IViewFor interface, etc.

The workflow: Define a ReactiveCommand, assign an async method with return value to it, and register a callback to be invoked after the command has been executed:

public ReactiveCommand<int> LoadNumber { get; }

private async Task<int> LoadNumberImpl(object param)
	// command business logic goes here, and returns anything...

public MyViewModel()
	LoadNumber = ReactiveCommand.CreateAsyncTask(this.LoadNumberImpl);
	LoadNumber.Subscribe(number => 
		// do something with the number returned by the command

The problem: The fact that ReSharper does not support me in creating the delegate within the .Subscribe method leads to the assumption that something is wrong. Still, try to type the whole thing manually…

The error message when trying to build: Cannot convert lambda expression to type ‘IObserver‘ because it is not a delegate type

I guessed on some missing namespace import at first sight, but after checking the rest of the ViewModel code it turned out that all the other Reactive classes, methods, etc. referenced were working perfectly.

What I did not know:

  • The ReactiveCommand class as well as all the WhenAny... extension methods are defined within the ReactiveUI namespace (which was correctly imported).
  • The ToObservable() extension method is defined within the System.Reactive.Threading.Tasks namespace (which was correctly imported).
  • All the Observable extension methods such as Select, Max, GroupBy, etc. are defined by the Observable class within the System.Reactive.Linq namespace (which was correctly imported).
  • The Subscribe(...) extension method is defined in the ObservableExtensions static class, which resides directly within the System namespace!

The solution: All ViewModels making use of String, DateTime, TimeSpan, or any other type from the System namespace were fine anyway. In the other, simply adding using System; solved the problem – and brought an end to e tedious debugging session…