2

I am trying to upgrade my project to support .NET 5 while still building on a .NET Core 3.1 toolset.

I tried to add the .NET 5 TFM (.net5.0) to my target frameworks like so.

<TargetFrameworks>netcoreapp3.0;netcoreapp3.1;net5.0</TargetFrameworks>

Unfortunately, this causes the build to fail when running on a machine with only .NET Core 3.1 toolset installed.

error MSB3971: The reference assemblies for ".NETFramework,Version=v5.0" were not found. You might be using an older .NET SDK to target .NET 5.0 or higher. Update Visual Studio and/or your .NET SDK.

I'm trying to find a condition so I can target net5.0 only if it is available, but documentation is sparse.

chyyran
  • 2,446
  • 2
  • 21
  • 35
  • I don't think MSBuild gives you a suitable condition to define. But in your build script you can detect the actual SDK versions available and modify the project file (removing .NET 5 target) on the fly – Lex Li Feb 11 '21 at 17:33
  • What is your exact goal here? What are you actually trying to do? Why do you want a project to be buildable by multiple SDK versions? Why not keep it at (eg) `netcoreapp3.1`? Separately, suppose you could build using 3.1 SDK. What is your expected outcome? Do you expect to be able to run the `net5.0` application there too? – omajid Feb 11 '21 at 18:38
  • My project multitargets `.netstandard2.0`, `.netcoreapp3.1` and `.net5.0`, with SDK-specific code defined out. For compatibility reasons, it should still be buildable against older SDKs with the `.net5.0` parts of the code defined away. – chyyran Feb 12 '21 at 19:49
  • How would you expect VS to build an assembly that can be use/consumed by some other .net 5 system if you don't have .net 5 on your system. Multi-targeting is referring to the artifact(s) your code produces, not the tools used to build it. As a metaphor, if you're a photographer, multi-targeting is offering your clients a roll of film and a usb of digitals. You can't make a usb of digitals if you only have a film camera. Your build env needs .net5 in order to make the artifact that will target .net5. – Josh Gust Mar 04 '22 at 23:36

1 Answers1

0

That's not really possible.

A new SDK (eg, 5.0) can target older runtimes via older TargetFrameworks (netcoreapp2.1, netcoreapp3.1, etc).

An older SDK can not target newer runtimes via new TargetFrameworks.

omajid
  • 14,165
  • 4
  • 47
  • 64
  • 1
    I don't intend to target newer runtimes, but instead disable targeting `.net5.0` and only target `.netcoreapp3.1` (for example), if `.net5.0` is not available. – chyyran Feb 12 '21 at 19:50