微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[备忘]处理错误:Your project does not reference ".NETFramework,Version=v4.5" framework. Add a reference to ".NETFramework,Version=v4.5" in the "TargetFrameworks" ...

错误信息:

Your project does not reference ".NETFramework,Version=v4.5" framework. Add a reference to ".NETFramework,Version=v4.5" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.

 

其中一种可能是同一个项目目录,被 .NET Framework 和 .NET Core 两个不同的解决方案引用,导致 obj 内的信息混乱。

 

解决方案一: 不要同时打开两个具有共享项目文件夹的解决方案,并且注意切换的时候删除 obj 和 bin 文件夹。

解决方案二:添加以下代码到 .NET Framework 的项目文件(.csproj)中:

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

 

更多讨论参考:https://stackoverflow.com/questions/52833741/your-project-does-not-reference-netframework-version-v4-6-2-framework-add-a

I experienced similar issue,but with v4.7.2. Namely,I kept getting build log message like this:

error : Your project does not reference ".NETFramework,Version=v4.7.2" framework. Add a reference to ".NETFramework,Version=v4.7.2" in the "TargetFrameworks" property of your project file and then re-run NuGet restore.

Despite the fact that it looked similar,none of the above proposed steps worked for me. I kept seeing this message after each build. nothing seemed to be able to help.

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.

You can try to fix the issue by putting the following snippet into your Framework project file:

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

It immediately worked for me,it was only later when I attentively read why we need it and why it works. I unexpectedly found it in part 2 of Migrating a Sample WPF App to .NET Core 3 under Making sure the .NET Framework project still builds section. BaSEOutputPath and BaseIntermediateOutputPath msbuild variables can be found there,not sure if they are documented well anywhere.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐