32

I have created a class library project and did some processing and also used Roslyn to generate code.

I use the library in a WPF GUI application as a reference.

These are the NuGet packages:

enter image description here

Build shows no error, however when I use the following code:

private static void GetGenerator()
{
  workspace = new AdhocWorkspace();
  generator = SyntaxGenerator.GetGenerator(workspace, LanguageNames.CSharp);
}

I get an exception:

"The language 'C#' is not supported."

   at: Microsoft.CodeAnalysis.Host.HostWorkspaceServices.GetLanguageServices(String languageName)
   at: Microsoft.CodeAnalysis.Host.Mef.MefWorkspaceServices.GetLanguageServices(String languageName)
   at: Microsoft.CodeAnalysis.Editing.SyntaxGenerator.GetGenerator(Workspace workspace, String language)

According to this and this, I have to copy the CodeAnalysis files locally and add the necessary references. They are there, yet the error occurs.

Is this still a bug that wasn't fixed in the last year?

What else should I do?

Community
  • 1
  • 1
Nestor
  • 8,194
  • 7
  • 77
  • 156

2 Answers2

34

Most likely it's because you don't reference Microsoft.CodeAnalysis.CSharp.Workspaces in your code, i.e. you never use a type or method in this dll, so MSBuild thinks it's not needed (see e.g. this question).

So what you could do is e.g. add the following line somewhere in your class library project:

var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions);

Then MSBuild should copy Microsoft.CodeAnalysis.CSharp.Workspaces.dll over and everything should be fine. No need to reference the NuGet packages from all the other projects.

Community
  • 1
  • 1
Johannes Egger
  • 3,874
  • 27
  • 36
  • 1
    In my case I was loading a solution via Roslyn in a standalone commandline app and all projects failed to load because I didn't have that package referenced in my app altogether. Adding a nuget reference fixed it. – Jeroen Vannevel Oct 12 '17 at 11:27
  • This would be a better solution than the accepted answer but isn't doable for all dlls. For example, Microsoft.CodeAnalysis.CSharp.Features.dll which is needed for code completion for C#, does not expose a single public type, so nothing to hold on to. The alternative is either a manual post-build event to copy the file, or the accepted answer of adding the Nuget packages to both the library and the referencing project. Yuck. The post-build action actually seems preferable to me. – anakic May 10 '20 at 17:26
  • Or option 3) add the required dlls as linked files into the referencing project and put "Copy to output directory" to "copy if newer". – anakic May 10 '20 at 17:33
  • This answer helped, sortof, In my case I added `Microsoft.CodeAnalysis.CSharp.Workspaces` to 2 projects but I'm guessing it failed on the main one and I didn't notice the add not being performed for both. – Stevie Jul 19 '21 at 08:51
23

You have to add the Microsoft.CodeAnalysis package to both the class library project AND the referencing project as well.

Nestor
  • 8,194
  • 7
  • 77
  • 156