Roslyn: Question: Can I use roslyn with .NET Core?

Created on 15 Apr 2018  路  8Comments  路  Source: dotnet/roslyn

I'm trying to use roslyn with .NET Core on Mac and I'm not having much luck because most of the resources are for .NET Framework on Windows.

I've installed Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.CSharp.Scripting, and Microsoft.CodeAnalysis.CSharp.Workspaces but VSCode says that I am missing assembly references for Microsoft.CodeAnalysis.CSharp.Scripting.

Are there any resources or documentation for using roslyn with .NET Core?

Area-Compilers Resolution-Answered

Most helpful comment

@nabeelomer Can you explain what exactly did you do and what specific error you're getting?

In general, I believe most of the Roslyn API should work fine on .Net Core, though I think there might be some exceptions regarding Workspaces.

For example, the following trivial usage of scripting works fine for me on .Net Core on Ubuntu 16.04:

  1. dotnet new console
  2. dotnet add package Microsoft.CodeAnalysis.CSharp.Scripting
  3. Open VS Code and change Program.cs to:

    ```c#
    using System;
    using Microsoft.CodeAnalysis.CSharp.Scripting;

    class Program
    {
    static void Main()
    {
    CSharpScript.EvaluateAsync("System.Console.WriteLine(\"Hello World\")").Wait();
    }
    }
    ```

  4. dotnet run, which prints "Hello World", as expected.

All 8 comments

Here are the instructions @svick provided on an earlier version of .NET Core. It's worth trying, but I suspect the integration changed in the meantime
Tagging @livarcocc @nguerrera

Are you asking about

  1. using the compiler on .net core, or
  2. using Roslyn API on .net core?

Those instructions linked above seem to be more about (1) and I suspect you're actually asking about (2).

For (1), latest RTM Roslyn compiler ships as part of .net core sdk. And you can use the Microsoft.NETCore.Compilers nupkg to upgrade the compiler used to build a given project. (Note NETCore vs. NET in nupkg name. Instructions linked above use NET, which would pull in a full framework compiler that would work only on Windows/mono but not .net core on Mac/Linux.)

@jcouv I didn't mean using a later version of newer version of the C# compiler with my project, I meant using the Roslyn APIs in my code. Sorry for the ambiguity.

@nguerrera I am asking about using the Roslyn API on on .NET Core (2). I apologize for the ambiguity.

@nabeelomer Can you explain what exactly did you do and what specific error you're getting?

In general, I believe most of the Roslyn API should work fine on .Net Core, though I think there might be some exceptions regarding Workspaces.

For example, the following trivial usage of scripting works fine for me on .Net Core on Ubuntu 16.04:

  1. dotnet new console
  2. dotnet add package Microsoft.CodeAnalysis.CSharp.Scripting
  3. Open VS Code and change Program.cs to:

    ```c#
    using System;
    using Microsoft.CodeAnalysis.CSharp.Scripting;

    class Program
    {
    static void Main()
    {
    CSharpScript.EvaluateAsync("System.Console.WriteLine(\"Hello World\")").Wait();
    }
    }
    ```

  4. dotnet run, which prints "Hello World", as expected.

@nabeelomer can you give us the exact errors you are seeing and the contents of the csproj file?

@svick I deleted my old project and made a new one following your steps and everything worked fine.

Then I'll go ahead and close the issue. Thanks

Was this page helpful?
0 / 5 - 0 ratings