33

I have an intensive Java background so forgive me if I'm overlooking something obvious in C#, but my research is getting me nowhere. I am trying to use the reactive Rx .NET library. The compiler is not complaining about the IObservable but it is with the call to the zip method. It is throwing the "... are you missing a using directive or assembly reference?"

I've been going through the namespaces and I cannot find what is looking for. I cannot find the System.Reactive which also throws an error if used, and all the references are already included for this Windows 8.1 app. Can someone please give me a lead on what is wrong?

public sealed class EventEngine
{    
    private static readonly EventEngine singleton = new EventEngine();

    public static EventEngine get()
    {
        return singleton;
    }

    public IObservable<MusicNote> CurrentKey { get; set; }
    public IObservable<Scale> CurrentScale { get; set; }

    public IObservable<AppliedScale> CurrentAppliedScale
    {
        get
        {
            return CurrentScale.zip(CurrentKey,
                (s, k) => AppliedScale.getAppliedScale(k, s));
        } 
    }

    private EventEngine() {}
}

*UPDATE *

Here is the working version after considering input from answers.

public sealed class EventEngine
{
    private static readonly EventEngine singleton = new EventEngine();

    public static EventEngine get()
    {
        return singleton;
    }

    public IObservable<MusicNote> CurrentKey { get; set; }
    public IObservable<Scale> CurrentScale { get; set; }

    public IObservable<AppliedScale> CurrentAppliedScale
    {
        get
        {
            return Observable.Zip(CurrentScale, CurrentKey,
                (s, k) => AppliedScale.getAppliedScale(s,k));
        } 
    }

    private EventEngine() {}
}
Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
tmn
  • 11,121
  • 15
  • 56
  • 112

2 Answers2

44

You have probably not added the necessary assembly references for Rx to your project. (Referencing an assembly is not the same thing as importing a namespace! You already know what a namespace is; an assembly is something similar to a JAR; the smallest unit of code deployment/distribution. Your project must reference it before the namespaces defined inside it become available for use.)

The compiler likely doesn't complain about IObservable<T> and IObserver<T> because your project is targeting .NET Framework version 4 or later. These two interfaces have been part of the core .NET Framework Class Library (FCL) since .NET version 4. (If you targeted an earlier .NET version, you'd get errors for using these undefined interfaces, too.)

Every part of Rx other than these two interfaces is not included in the core .NET FCL, but resides in their own (add-on) assemblies. You can add them to your project e.g. by installing the corresponding NuGet packages:

  1. In Visual Studio, go to ToolsNuGet Package ManagerPackage Manager Console.

  2. In the NuGet console window, select the target project (where you want to use Rx) in the drop-down list Default project.

  3. Next, type Install-Package System.Reactive and hit Enter ↵. (Note: This package used to be called Rx-Main previously; see this answer for details.)

  4. This will add the System.Reactive.* assembly references to your project.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 3
    I was confused because I got the impression the reactive library was inherently part of the .NET core framework just because the `IObservable` was. Apparently it is not. – tmn May 08 '15 at 02:00
  • 7
    The information around RX.NET that is published on both the official GitHub and MSDN is insanely confusing. As someone just getting into this now, I don't even know how anyone can have the patience to get started with this... MSDN states that the core of it is included (obviously referring to IObservable, etc) and doesn't mention one thing about the rest of it. In fact they send you in a hyperlink loop (with no actual downloads) if you ever try to install it: https://msdn.microsoft.com/en-us/library/hh242980(v=vs.103).aspx – Adam Plocher Jul 09 '17 at 00:38
  • @AdamPlocher Yup, thats my experience exactly. – John Zabroski Aug 24 '20 at 23:50
  • Documentation/guides on Reactive itself are so clean and thorough but this is probably the most trouble I've ever had getting to "Hello World" ^_^;; – The Most Curious Thing Mar 30 '21 at 09:14
33

As of version 3, the Rx project is now known as System.Reactive because it "brings the NuGet package naming in line with NuGet guidelines and also the dominant namespace in each package."

It can be downloaded from NuGet by searching for "System.Reactive" or you can download it here: https://www.nuget.org/packages/System.Reactive/

Here is the project's GitHub page: https://github.com/Reactive-Extensions/Rx.NET

Package Mappings

  • Rx-Main System.Reactive
  • Rx-Core System.Reactive.Core
  • Rx-Interfaces System.Reactive.Interfaces
  • Rx-Linq System.Reactive.Linq
  • Rx-PlatformServices System.Reactive.PlatformServices
  • Rx-Testing Microsoft.Reactive.Testing

Source

Matt Klein
  • 7,856
  • 6
  • 45
  • 46
  • 4
    What if you need to install it for .NET framework v4.0? – Michal Ciechan Oct 12 '16 at 17:14
  • 1
    @MichalCiechan you can get [this version](https://www.nuget.org/packages/System.Reactive.PlatformServices/). It works for all frameworks (>= 3.1.1). – Rogerson Nazário May 03 '17 at 20:25
  • This is easily the most time wasting library in all of .NET. All I want to do is grab a Buffer extension method , so i search Google for "system observable buffer" and the third link is https://learn.microsoft.com/en-us/previous-versions/dotnet/reactive-extensions/hh212130(v=vs.103) which doesn't actually exist when I go to download the Nuget package. Then I find this StackOverflow post mocking me with the complexity of the package mappings. And Visual Studio won't auto-suggest anything for AsObservable(). – John Zabroski Aug 24 '20 at 23:43