none
Use RX 2.0 RTM in F# RRS feed

  • Question

  • Hello,

    I saw RX 2.0 RTM is out, so I want to give it a try.

    Since my C# level is not good, so I want to see if I can use RX in F#.

    I created a F# Windows application and reference most of the RX assemblies:

    System.Reactive.Interfaces, System.Reactive.Core, System.Reactive.Linq, System.Reactive.PlatformServices.

    However, my code could NOT get compiled:

    #light

    open System

    open System.Collections.Generic

    open System.ComponentModel

    open System.Linq

    open System.Text

    open System.Reactive

    open System.Reactive.Linq

    let input = Observable.Range(1, 100)

    input.Sum().Subscribe(printfn "%A")

    I got compiler errors:

    Error 1 A reference to the type 'System.IObservable`1' in assembly 'System.Runtime' was found, but the type could not be found in that assembly C:\Temp\Fsharp\rx02\rx02\Program.fs

    Error 2 A reference to the type 'System.Action`1' in assembly 'System.Runtime' was found, but the type could not be found in that assembly C:\Temp\Fsharp\rx02\rx02\Program.fs

    Error 3 A reference to the type 'System.IObservable`1' in assembly 'System.Runtime' was found, but the type could not be found in that assembly C:\Temp\Fsharp\rx02\rx02\Program.fs

    Error 4 The value, constructor, namespace or type 'Range' is not defined C:\Temp\Fsharp\rx02\rx02\Program.fs

    Error 5 Lookup on object of indeterminate type based on information prior to this program point. A type annotation may be needed prior to this program point to constrain the type of the object. This may allow the lookup to be resolved. C:\Temp\Fsharp\rx02\rx02\Program.fs           Error 6 The type 'Lazy`2' is required here and is unavailable. You must add a reference to assembly 'System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

    But the C# version of the same program works without any issue.

    I want to know if I can use RX 2.0 for F# now or do I have to add something else.

    My development environment is MS Visual Studio 2012 and the assembly version for RX is: 2.0.20823.0.  OS version: Windows 2008 R2 with SP1

    Thanks,

    John

    Thursday, September 13, 2012 8:35 PM

Answers

  • Hi John,

    I was able to repro your issue.  You are simply missing an assembly reference: System.ComponentModel.Composition.dll.  After adding that reference from the Assemblies > Framework tab in the Add Reference dialog your code ran successfully.

    When you get type or assembly binding errors in the future, try to resolve all of them.  It seems that this one missing reference prevented the other references from being loaded, thus producing secondary errors that didn't make sense.

    - Dave


    http://davesexton.com/blog

    • Proposed as answer by Dave Sexton Friday, September 14, 2012 10:16 PM
    • Marked as answer by zydjohn Saturday, September 15, 2012 7:23 AM
    Friday, September 14, 2012 10:16 PM

All replies

  • Hi John,

    Is your F# project targeting the same version of the .NET Framework as the Rx assemblies that you referenced?

    - Dave


    http://davesexton.com/blog

    Friday, September 14, 2012 3:28 AM
  • Hi,

    For RX 2.0.20823, which was published on September 11, 2012, the only supported target Framework is .NET Framework 4.5; if I change the target Framework to .NET Framework 4, then all RX references become invalid, a big yellow icon appear on them.
    But if I use .NET Framework 4.5, in F#, I can't get rid of the compiler errors.
    If I use the same reference, the following C# code works:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Reactive;
    using System.Reactive.Linq;

    namespace CSharpRX01
    {
        class Program
        {
            static void Main(string[] args)
            {
                var input = Observable.Range(1, 100);
                input.Sum().Subscribe(x => Console.WriteLine("The Sum is {0}", x)); 
            }
        }
    }



    The result is:
    The Sum is 5050

    Any idea why in F#, RX 2.0 RTM not working?

    Thanks,

    John


    Friday, September 14, 2012 7:56 PM
  • Hi John,

    I was able to repro your issue.  You are simply missing an assembly reference: System.ComponentModel.Composition.dll.  After adding that reference from the Assemblies > Framework tab in the Add Reference dialog your code ran successfully.

    When you get type or assembly binding errors in the future, try to resolve all of them.  It seems that this one missing reference prevented the other references from being loaded, thus producing secondary errors that didn't make sense.

    - Dave


    http://davesexton.com/blog

    • Proposed as answer by Dave Sexton Friday, September 14, 2012 10:16 PM
    • Marked as answer by zydjohn Saturday, September 15, 2012 7:23 AM
    Friday, September 14, 2012 10:16 PM
  • Hi John,

    Note that Rx 2.0.20823 can install assemblies for .NET 4.0 and 4.5, but the .NET 4.0 assemblies are not installed by default.  You'd have to choose "Custom" in the installer dialog and then enable .NET 4.0.

    - Dave


    http://davesexton.com/blog

    Friday, September 14, 2012 10:17 PM
  • Hi, Dave:

    Thank you very much, your suggestion works.

    However, F# compiler still report compile errors even it can produce correct result.  I have to save all and rebuild solution to get rid of compile errors, this could be a small bug.  After I close the solution, the second time I open it, the compile errors still shown: there are red wavy lines under all the F# codes.

    But in C# code, there is no red wavy lines, and I don't have to add reference for 

    System.ComponentModel.Composition

    Any way, thank you very much for your good suggestion.

    Have  a nice day!

    John

    Saturday, September 15, 2012 7:30 AM
  • I do have the same problem. Adding the 4.5 rx dlls to a 4.5 f# application gets following error on "open System":

    "The type 'IObservable`1' is required here and is unavailable. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'."

    Adding system.componentmodel.composition lets me compile correctly but intellisense still shows all errors.

    Workaround: Creating a 4.0 application, adding the nuget rx dlls (4.0) and upgrading to 4.5 works tough. 

    What are the implications of doing this since rx is a portable library?

    Another way might be to use the Workaround and just change the rx dlls to 4.5 and add componentmodel.composition ast last.

    Any ideas?

    Ah, "saving all" and rebuilding also gets rid of the intellisense errors as John has said.

     
    Tuesday, October 30, 2012 9:02 AM
  • There are some known issues with Portable Library assemblies used from F#. We'll bring this to the attention of the Portable Library team to make sure it's on the radar for their future releases or updates.
    Monday, November 5, 2012 3:10 AM
  • There are some known issues with Portable Library assemblies used from F#. We'll bring this to the attention of the Portable Library team to make sure it's on the radar for their future releases or updates.

    Just wanted to add that as far as i can tell it's not possible to reference the rx portable library from a f# portable library project.

    When installing from nuget:

    Install failed. Rolling back...
    Could not install package 'Rx-Interfaces 2.0.21103'. You are trying to install this package into a project that targets '.NETPortable,Version=v4.0,Profile=Profile47', but the package does not contain any assembly references that are compatible with that framework. For more information, contact the package author.

    Any ways around that?

    Edit: I get the same error when trying to reference from a c# portable library project (tested 4-5 target framework combinations).
    Tuesday, November 6, 2012 2:52 PM