C# WPF Project cannot call F# library

.net, f#, wpf

Solution

It seems you found the answer already -- building the F# project -- but I'm going to provide an answer (for posterity) to a problem which would have almost the exact same symptoms, but a different underlying cause.

If you use your example F# code exactly as provided, that is, a module with a simple name (without a namespace), it's important to know that you need to use the `global` keyword when accessing the F# module from C#. For example, this is how you'd have to call the `Add` function you defined from C#:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var result = global::MyModule.Add(1, 2);
        }
    }
}

Unless you have a specific reason not to, it's usually just a better idea to use a namespace with your module, e.g., `module MyNamespace.Module1`.

Problem

Recently I took a 2 week hiatus from my current project to write a decent sized file parser and a numerical error checker. I decided to write them in F# for kicks and giggles. Fantastic decision. The older version of the program written in VB was over 1000 lines; I managed it in 170 in F#. Awesome. I'm back on my current project now and want to incorporate an F# lib to do some parsing and maybe writing/reading an XML save file. But I cannot seem to figure out how to call an F# library from a C# WPF application for the life of me. I'm using Microsoft Visual 2010 Professional, here's what I've attempted thus far referencing this article: http://www.devjoy.com/2013/02/c-to-f-interop-sharing-a-domain-model/ and a couple SO posts: - Create a WPF project. - Added an F# library project to the solution. - Added a reference to the F# library to C# project via: Right click project file in SolutionExplorer, click 'Add Reference', selected 'MyFSharpLib' from projects tab and click Add. I then added some test code to the default F# file module1.fs: ``` module Module1 let Add a b = a + b ``` And tried to call it from C#: ``` using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication2 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } // Call F# library private void Button_Click(object sender, RoutedEventArgs e) { int a = Module1.Add(1, 2); } } } ``` This results in an error saying: "The name 'Module1' does not exist in the current context." I think the problem is with the assemblies. I noticed in a couple code samples I've seen have `using Microsoft.FSharp.Core`. Is this the source of my grief? I tried adding it and couldn't find it under the .NET tab. This is driving me crazy, any help would be much appreciated.

Original source