存在 ProjectReferences 时,不填充 Project MetadataReferences [英] Project MetadataReferences is not populated when ProjectReferences are present

查看:94
本文介绍了存在 ProjectReferences 时,不填充 Project MetadataReferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在加载 MSBuildWorkspace 中的解决方案:

I'm loading in a solution in the MSBuildWorkspace:

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

没有 ProjectReferences 的项目显示所有 MetadataReferences,包括 mscorlib.带有 ProjectReferences 的项目有一个空的 MetadataReferences 集合.

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

当编译工作时,我猜编译器平台出于某种原因没有填充这个集合,我想知道为什么?如果我删除 ProjectReferences,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.

诊断包含缺少 mscorlib 类型(如 Object 和 DateTime)的错误,因此项目似乎由于缺少这些引用而无法编译.

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

推荐答案

在尝试打开解决方案后,我通过检查 MSBuildWorkspace 对象上的 .Diagnostics 终于弄清楚发生了什么:

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'.

似乎正在使用不同版本的 MSBuild.将程序集重定向(从 C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe.config 复制)到我的项目 app.config 解决了它:

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.

我使用的是 VS2017 15.7.1 并面向 .NET 4.7.1,因此在 Microsoft.CodeAnalysis 包之上不需要 NuGet 包(我使用的是 2.8.0).

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).

这篇关于存在 ProjectReferences 时,不填充 Project MetadataReferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆