錯誤信息:
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>
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 putproject.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
andBaseIntermediateOutputPath
msbuild variables can be found there, not sure if they are documented well anywhere.