1

I have a library that I used to build against different versions of .NET by executing a command line like this: msbuild x.csproj /p:TargetFrameworkVersion=v4.6.2 ... multiple times with different value for the property TargetFrameworkVersion. Now I wanted to add .NET 4.7.1 to the script. The build for 4.7.1 succeeds but the next builds fail with a message like this:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets(186,5): error : Your project is not referencing the ".NETFramework,Version=v4.6.2" framework. Add a reference to ".NETFramework,Version=v4.6.2" in the "frameworks" section of your project.json, and then re-run NuGet restore. [x.csproj]

I searched for this message a lot but could not find any useful solutions that would fix my build script. The closest to a solution was this. But deleting project.assets.json before each msbuild did not help. Apparently there is a problem with restoring the NuGet packages for the different versions of .NET. Can you please suggest a solution.

Valo
  • 1,872
  • 2
  • 15
  • 23
  • BTW, why do you need to build against different versions of .NET at all? In most cases, you should compile against a single .NET Framework version, and then other platforms (.NET Standard, Xamarin for example). There is no real value from what you do right now. – Lex Li Mar 21 '18 at 03:53
  • Have you tried manually deleting the bin & obj folders of that project not just clean the build? – Leo Liu Mar 21 '18 at 12:23
  • @Lex Li, because there are people on older .NETs (e.g. 4.5) that use the library, but I would like to move ahead and be current on it. – Valo Mar 22 '18 at 00:15
  • @Leo Liu, yes I did try it and the effect is the same as deleting just the project.json file: works on 4.7.1 and fails on earlier versions. – Valo Mar 22 '18 at 00:16
  • An assembly built against .NET Framework 4.5 can work flawlessly on later versions (unless you really would like to conditionally compile for some very special cases). – Lex Li Mar 22 '18 at 00:17

1 Answers1

0

OK, what I ended up doing is to manually add a line like this <TargetFrameworkVersions>net452;net462;net471</TargetFrameworkVersions> in the first PropertyGroup element of the project file. And then to modify (not to delete) the file project.assets.json by adding sections for the various targeted .NET versions. It is tedious work and I can imagine this becoming a pain in the tail for multiple projects with many versions. I saved a copy of the file somewhere in the project and added it to the repository, so that if it gets regenerated by VS - I can easily restore it.

If you can stick to one version only (the least common denominator) - you don't need any of it. Here is my project.assets.json file for a very simple structure and minimum number of dependencies project published on NuGet for .NET 4.5.2, 4.6.2 and 4.7.1. Let's hope that the VS team will find a better way for doing all this in the future.

Valo
  • 1,872
  • 2
  • 15
  • 23