我不能运行单元测试。

我有下一个错误:

你的项目没有引用“.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文件夹,并在属性窗口中选择所需的目标框架后重新运行构建,它为我工作。

其他回答

git clean -xdf

这样应该可以了。这在Jenkins中也适用。(我们只是用一个修改过的脚本重播了失败的构建,首先运行git clean)。

出于某种原因,MSBuild / Visual Studio在切换针对不同版本的。net框架的分支时感到困惑,所以我不得不在分支之间切换时定期进行git清理。

不管出于什么原因,我在VS2022中得到了这个构建错误。

在VS2019中同样的构建是成功的。

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

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

<ResolveNuGetPackages>false</ResolveNuGetPackages>

在我的例子中,删除project文件夹works下的.pkgrefgen/文件夹,它包含一个引用旧的。net框架的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变量可以在那里找到,不确定它们是否在任何地方都有良好的文档。