Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Project MetadataReferences is not populated when ProjectReferences are present

Tags:

c#

roslyn

I'm loading in a solution in the MSBuildWorkspace:

var msWorkspace = MSBuildWorkspace.Create();
var solution = msWorkspace.OpenSolutionAsync(solutionPath).Result;

Projects without ProjectReferences show all MetadataReferences, including mscorlib. Projects with ProjectReferences have an empty collection of MetadataReferences.

As compilation works, I guess the compiler platform for some reason is not populating this collection, and I'm wondering why? If I remove the ProjectReferences, the MetadataReferences collection gets populated correctly.

EDIT: Diagnostics contains errors for missing mscorlib-types like Object and DateTime, so the project seems to not compile because of these missing references.

like image 789
nikolaia Avatar asked Apr 24 '18 07:04

nikolaia


People also ask

How do I fix missing references in Visual Studio?

To fix a broken project reference by correcting the reference path. In Solution Explorer, right-click your project node, and then select Properties. The Project Designer appears. If you're using Visual Basic, select the References page, and then click the Reference Paths button.

How do you add a reference to a project?

Add a reference In Solution Explorer, right-click on the References or Dependencies node and choose either Add Project Reference, Add Shared Project Reference, or Add COM Reference. (You can right-click the project node and select Add from the fly-out menu to choose from these options, too.)

How do I add a project reference in Csproj?

To add a reference, right click on the References or Dependencies node in Solution Explorer and choose Add Reference. You can also right-click on the project node and select Add > Reference.


1 Answers

I finally figured out what was going on by checking the .Diagnostics on MSBuildWorkspace object after attempting to open the solution:

[Failure] Msbuild failed when processing the file 'c:\xxx\someproject.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets: (84, 5): The "Csc" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.CodeAnalysis.BuildTasks.Csc' to type 'Microsoft.Build.Framework.ITask'.

It seems different versions of MSBuild were being used. Adding assembly redirects (copied from C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config) to my projects app.config solved it:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Framework" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Conversion.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Tasks.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Utilities.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Engine" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Conversion.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

All references are now correctly populated on every project in the solution.

I'm on VS2017 15.7.1 and targeting .NET 4.7.1 so no NuGet packages were needed on top of the Microsoft.CodeAnalysis packages (I'm using 2.8.0).

like image 119
nikolaia Avatar answered Oct 21 '22 08:10

nikolaia