在分支之间转换时构建错误:您的项目没有引用“.NETFramework,Version=v4.7.2"框架 [英] Build error while transitioning between branches: Your project is not referencing the ".NETFramework,Version=v4.7.2" framework

查看:45
本文介绍了在分支之间转换时构建错误:您的项目没有引用“.NETFramework,Version=v4.7.2"框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 Git,并且我们有一个针对完整网络框架的解决方案.几天前,我开始将解决方案迁移到 .net 核心.不幸的是,出现了一些事情让我回到 master 分支(它有完整的 .NET 框架的代码).每当我尝试构建应用程序时,都会收到以下错误:

<块引用>

1>D:Program Files (x86)Microsoft VisualStudio2017CommunityMSBuildMicrosoftNuGet15.0Microsoft.NuGet.targets(186,5):错误:您的项目没有引用.NETFramework,Version=v4.7.2"框架.添加引用.NETFramework,Version=v4.7.2"在您的框架"部分project.json,然后重新运行 NuGet 还原.

我尝试清理 nuget 包,运行 git reset,但似乎没有任何帮助.知道发生了什么吗?

解决方案

通过自定义 Visual Studio 自动删除非核心项目的 project.assets.json 解决此问题构建事件.

更新(2020 年 6 月 13 日)结果证明删除 project.assets.json 会导致显示波浪线,因为 Intellisense 需要文件中的引用.因此,更好的解决方法是使用预构建事件仅在项目不是.Net Core时删除文件..>

这由 $(TargetFramework) --->netcoreapp3.1" 在我的电脑上.您安装的框架可能会显示不同的标识符,因此相应地更新脚本(请参阅第 2 行 ECHO 生成的构建 输出 窗口中的文本).注意:这在某些 .Net Framework 版本上可以是空字符串,这应该不是问题.我们也只比较前 7 个字符以忽略版本,以避免在版本更改时/当版本更改时必须更新脚本.

SET _tgt=$(TargetFramework)回声%_tgt%如果不是 "%_tgt:~0,7%" == "netcore" (cd $(ProjectDir)objDEL project.assets.json)

==== 更新 (6/13/2020) 到此结束.原始答案保留在下方以提供上下文. ====

我们将问题缩小到一个文件:{Your project}/obj 文件夹中的 project.assets.json.它是由 .Net Core 项目创建的文件,但在切换到 .Net Framework 项目后,Visual Studio 不会将其删除,从而导致 OP 提到的问题.>

解决方案是删除此文件,但不必每次手动删除它,我们需要切换项目,而是在 Visual 中创建了一个 post-build 事件Studio 在每次成功的 Core 构建之后 将其删除(如果您在之前运行脚本,您的 Core 项目将不会构建> 显然是构建).您可以自定义脚本以删除您认为有问题的任何文件/文件夹,但我们的问题仅限于该单个文件.

cd $(ProjectDir)obj删除 project.assets.json

注意:如果有问题的工件已经存在,您将需要手动删除它一次,因为构建后事件只会在构建成功后运行.

We're using Git and we have a solution which is targeting the full net framework. A couple of days ago, I've started migrating the solution to .net core. Unfortunately, something comes up which made me go back to the master branch (which has the code for the full .NET framework). Whenever I try to build the app, I get the following error:

1>D:Program Files (x86)Microsoft Visual Studio2017CommunityMSBuildMicrosoftNuGet15.0Microsoft.NuGet.targets(186,5): error : Your project is not referencing the ".NETFramework,Version=v4.7.2" framework. Add a reference to ".NETFramework,Version=v4.7.2" in the "frameworks" section of your project.json, and then re-run NuGet restore.

I've tried cleaning up the nuget packages, running a git reset, but nothing seems to help. Any idea on what's going on?

解决方案

Fix this by automatically deleting project.assets.json for non-Core project(s) via a custom Visual Studio Build Event.

Update (6/13/2020) It turned out deleting project.assets.json caused squiggy lines to show because Intellisense needed the references from the file. So an even better fix is to use a Pre-build event to only delete the file if the project is not .Net Core.

This is identified by $(TargetFramework) ---> "netcoreapp3.1" on my computer. Your installed framework might show a different identifier so update the script accordingly (see the text in your build Output window generated by the ECHO on line 2). Note: This can be an empty string on certain .Net Framework version(s) which shouldn't be an issue. We're also only comparing the first 7 characters to ignore the version to avoid having to update the script if/when the version changes.

SET _tgt=$(TargetFramework)
ECHO %_tgt%
IF NOT "%_tgt:~0,7%" == "netcore" (
    cd $(ProjectDir)obj
    DEL project.assets.json
)

==== Update (6/13/2020) ends here. Original answer kept below to provide context. ====

We narrowed down the problem to a single file: project.assets.json in the {Your project}/obj folder. It's a file created by a .Net Core project but it does not get deleted by Visual Studio after switching to a .Net Framework project causing the issue mentioned by OP.

The solution is to remove this file but, rather than having to delete it manually every time we need to switch projects, we created a post-build event in Visual Studio to remove it after each successful Core build (your Core projects won't build if you run the script before the build, obviously). You can customize the script to delete whatever files/folders you deem to be problematic but our issue was limited to that single file.

cd $(ProjectDir)obj
del project.assets.json

Note: You will need to delete the offending artifact(s) manually once if it already exists since the post-build event will only run after a successful build.

这篇关于在分支之间转换时构建错误:您的项目没有引用“.NETFramework,Version=v4.7.2"框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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