C# - F# Interop support in Visual Studio 2015 on .

2019-01-26 04:43发布

问题:

Note: here I talk about dotnet core and not full framework! For full framework there are plenty of docs on this, but this is dotnet core.

I have an F# library created by

dotnet new --lang F#

And an ASP.NET Core RC2 C# application (created by File->New Project in VS).

Now I use my F# library from C# (I guess a very common scenario…). The F# project is referenced in the project.json as any other .net library (by listing it under dependencies)

Now all this compiles and works fine, which is great!

The only problem is that VS does not seem to support it. There is no intellisense and lines, where I use the F# functions from C# are marked as error. Debugging from C# to F# does not work either. I tried it on a Mac with VS Code, same there…

When I hit compile, the compilers figure this out and everything is fine.

This screenshot summarizes it (this is where I call an F# function from C#):

So basically I ended up with VS solution with a bunch of errors in it, which still compiles.

Here is a simple reproducer.

Questions:

  1. Should intellisense and debugging work in VS at all?
  2. If yes, what did I do wrong?
  3. If no, is this scenario planned to be covered in the future?

Here is the code from the reproducer:

F# library - project.json (created by cli - dotnet new --lang F#)

  "version": "1.0.0-*",
  "buildOptions": {
    "compilerName": "fsc",
    "compile": {
      "includeFiles": [
        "FSharpLibrary.fs"
      ]
    }
  },
  "dependencies": {
    "Microsoft.FSharp.Core.netcore": "1.0.0-alpha-160316",
    "NETStandard.Library": "1.5.0-rc2-24027"
  },
  "tools": {
    "dotnet-compile-fsc": {
      "version": "1.0.0-*",
      "imports": [
        "dnxcore50",
        "portable-net45+win81",
        "netstandard1.3"
      ]
    }
  },
  "frameworks": {
    "netstandard1.5": {
      "imports": [
        "portable-net45+win8",
        "dnxcore50"
      ]
    }
  }

F# library, the code:

namespace FSharpLibrary

module Sample =

    let public FSharpFunction number = 
            printfn "Hello From F#, %d" number

C# application (here I use a console app, but same things as with asp.net core)

using System;
namespace ConsoleAppCSharp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            FSharpLibrary.Sample.FSharpFunction(42); //<- This is red. and marked as error.     
            Console.ReadKey();
        }
    }
}

C# console application project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "LibraryFSharp": "1.0.0-*",
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

Thanks

回答1:

So currently what I described above is all the support you have in Visual Studio 2015 with the preview tooling. Visual Studio 15 will be hopefully better, but the current Preview (Preview 5) does not have .NET Core support at all.

What is worth trying out is Visual Studio Code. In F# you get intellisense for type which are defined in C#. So for F#-C# interopability currently VS Code has much better support than the full VS.