我不能运行单元测试。

我有下一个错误:

你的项目没有引用“.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)

我该怎么解决呢?


当前回答

请做下一步

清洁解决方案 清洁文件夹“包” 删除文件夹“bin” 删除文件夹"obj"

其他回答

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

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

我在。net 4.71中遇到了同样的问题。在我的例子中,我只是从包迁移。配置为“包引用”

从包迁移。配置到PackageReference

…它解决了我的问题。对于我来说,我无论如何都要这样做,所以如果您已经这样做了,我就跳过上面的步骤,迁移到包引用。

请做下一步

清洁解决方案 清洁文件夹“包” 删除文件夹“bin” 删除文件夹"obj"

我也遇到过类似的问题,但使用的是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文件