我不能运行单元测试。

我有下一个错误:

你的项目没有引用“.NETFramework,Version=v4.6.2” 框架。在。net framework,Version=v4.6.2中添加一个引用 “TargetFrameworks”属性,然后重新运行NuGet 恢复。

在app.config:

<startup>
  <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>

在项目>属性>应用>目标框架(。NET Framework 4.6.2)

我该怎么解决呢?


当前回答

引起问题的文件是obj/project.assets。Json在项目文件夹中。删除它并重新构建项目成功了。

其他回答

我也遇到过类似的问题,但使用的是v4.7.2。也就是说,我一直得到这样的构建日志消息:

错误:你的项目没有引用“. net framework,Version=v4.7.2”框架。在项目文件的“TargetFrameworks”属性中添加对“. net framework,Version=v4.7.2”的引用,然后重新运行NuGet还原。

尽管看起来很相似,但上面建议的步骤对我都不起作用。每次构建之后,我都会看到这条消息。似乎什么也帮不上忙。

In fact, the problem was related to that, due to migration, I had to put two projects in one folder of code. One of them was targeted at .Net Core, another at .Net Framework, both referenced same .Net Standard libraries. Apparently, they share the same obj folder where Core projects put project.assets.json file. Actually, exactly this file interferres with the Framework project preventing its normal build. Seems even if you performed Migrate from packages.config to PackageReference... which was recommended as one of possible solution.

你可以尝试通过将下面的代码片段放入你的框架项目文件来修复这个问题:

<Project>
  ...
  <PropertyGroup>
    <BaseOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/bin</BaseOutputPath>
    <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/out/$(MSBuildProjectName)/obj</BaseIntermediateOutputPath>
  </PropertyGroup>
  ...
</Project>

它立刻对我起了作用,直到后来我仔细地读到为什么我们需要它,为什么它起作用。我意外地在《确保。net框架项目仍然构建》的第2部分将一个样本WPF应用程序迁移到。net Core 3中发现了它。BaseOutputPath和BaseIntermediateOutputPath msbuild变量可以在那里找到,不确定它们是否在任何地方都有良好的文档。

在我的例子中,删除project文件夹works下的.pkgrefgen/文件夹,它包含一个引用旧的。net框架的project.assets.json文件

在VS2017中打开VS2015项目时,我就遇到了这种情况。从obj文件夹中删除project.assets.json就可以了。

无论如何,该消息的框架在文件中丢失了,我没有添加它,尽管删除了它。

重命名项目为我解决了这个错误。问题发生在我创建。net Core项目之后,然后我删除了它,并创建了一个同名的。net Standard项目。Obj文件夹根本不存在。删除bin文件夹,包,清洁和重建解决方案和获得最新的覆盖没有帮助。

我没有尝试过,但是这个线程提出的解决方案是包括在csproj标签中:

<ResolveNuGetPackages>false</ResolveNuGetPackages>

问题:在VS2017。缺少对。netframework 4.5.2的引用,尽管它被引用为目标框架。

我的解决方案:安装验证框架并重新启动机器。在git清理后,只需在探索和“恢复nuget包”中右键单击解决方案就可以了。