Gergely Kalapos


Referencing an F# library from C# on .NET Core

Posted on July 03, 2016



Aaaand a post again, which will be deprecated at some point: currently using both F# and C# in a .net core solution is possible, but needs some workaround. Here I describe how you can do it. In the sample I create an asp.net core C# application, and an F# library, which is used from the asp.net core C# app. The sample is here.

It's a very common scenario that you want to create a .NET library in F# and use it from C#. On Full Framework this is trivial. Visual Studio has templates for all of this, you create a library by clicking to "File" -> "New Project" -> "F# tab" -> F# library. Now this does not work on CoreCLR. These templates use the old project system, so they all target full framework.

If you want to do it for CoreCLR here are the steps:

  1. Create an F# library with the command line tool


    At this point (right after the RTM release of CoreCLR) there is no template for coreCLR based F# projects in visual studio, so you have to create it with the dotnet cli by typing:

    dotnet new --lang F#

    This generates a coreCLR based F# console application.



    The project.json is basically already fine, one thing, which you can change is to set the  "emitEntryPoint" to false.

    As a sample I only created a single function, which returns a string and has an int parameter:

    namespace FSharpLib
    
    module FSharpSample =
    
        let SampleFSharpFunction (n: int) =
            "Hello From F#, you passed: " + n.ToString()
    
  2. Add the generated F# lib to a Visual Studio solution

    If you work on Windows with high probability you use Visual Studio and you want to have the F# library in your solution. For this you can just right click the solution and select "Existing project". Here you can select the project.json file and open it. VS automatically generates an .xproj file in the folder.



    After this you have the F# project in your solution. This basically lists the F# library under the "dependencies" section of the project.json of the C# project.



    If you are on a non-Windows OS, you can do this step manually, just list the fsharp library as a dependency.
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.0.0",
          "type": "platform"
        },
        "Microsoft.AspNetCore.Diagnostics": "1.0.0",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "LibFSharp": "1.0.0-*"
      },
  3. Use the F# library from C#

    From this point you can use the F# library from C# as you use any F# lib from C# on full framework. In the sample I return the text from the F# library as the response for any http request:
public void Configure(IApplicationBuilder app,
   IHostingEnvironment env, ILoggerFactory loggerFactory)
{
     loggerFactory.AddConsole();

     if (env.IsDevelopment())
     {
          app.UseDeveloperExceptionPage();
     }

     app.Run(async (context) =>
     {
          var txt = FSharpLib.FSharpSample.SampleFSharpFunction(42);
          await context.Response.WriteAsync(txt);
     });
}

Limitations

There are two things which are in my opinion important, but do not work at this point:

  • The Visual Studio (and Visual Studio Code) support is very limited: VS marks every line as an error, although the compilers can deal with the solution in multiple languages. At this point we have to live with this.
  • Debugging from C# to F# and vice versa does not work.

I tried to figure out if there is a solution for these problems on StackOverflow, but as it seems we have to accept this. I work on a side project where I have an F# library in my solution and with the listed limitations I'm still productive, so if you accept it you can already use F# in a real project on .net core.


;