Roslyn AdhocWorkspace asking for reference to System.Runtime

Viewed 704

I'm trying to do semantic analysis on a bit of C# code. When setting up an AdhocWorkspace for my tests I've found examples that point to using the the assembly location on Type and object. When I do this I get the following errors in my diagnostics:

[0]: TestIsNotInstanceOfTypeMessage.cs(13,20): error CS0012: The type 'Object' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

[1]: TestIsNotInstanceOfTypeMessage.cs(13,13): error CS0012: The type 'Type' is defined in an assembly that is not referenced.You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

When I add the following line everything works well:

MetadataReference.CreateFromFile(@"C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\v4.6.1\\Facades\\System.Runtime.dll"),

I'm not really happy with a hard coded path in my test project but am unsure how to "get" the 4.6.1 versions of object and Type any other way. The code below shows the gist of my helper to get an AdhocWorkspace

private static readonly ImmutableArray<MetadataReference> _coreReferences = 
ImmutableArray.Create<MetadataReference>(
MetadataReference.CreateFromFile(typeof(Type).Assembly.Location),
MetadataReference.CreateFromFile(typeof(object).Assembly.Location)


private static readonly Project _baseProject = new AdhocWorkspace()
    .AddProject("Test", LanguageNames.CSharp)
    .AddMetadataReferences(_coreReferences)
    .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

Some more details:

  • Visual Studio Community v15.4.0
  • Target framework: .NET 4.6.1
  • Microsoft.CodeAnalysis.CSharp v2.4.0
  • The main project is a console application, and the test project is a regular library, neither are PCLs
1 Answers

This is due to the fact that you are using AdhocWorkspace. This workspace is designed to be .Net-agnostic as far as I know. Meaning - it should work for both .Net Standard and .Net Core, cross version.

If you want to skip issues related to default libraries, you may be better off with MSBuildWorkspace

However, this workspace relies on MSBuild being installed on the machine, and some other MSBuild specific things which is likely to lead to some other issues. Like:

Msbuild failed when processing the file 'C:\Path\Myfile.csproj' with message: C:\Path\packages\somepackage\build\somepackage.targets: (75, 2): The "sometask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

If you want to stick to AdHoc workspace - you will have to locate the references manually, by using the registry to locate the .Net assemblies folder or maybe turn to the GAC. In other words - if you want to tie the AdHoc space in your solution to the .Net Standard - you're allowed to do so, but you are on your own.

Related