When .NET Core still used the project.json
format, you could build a class library targeting multiple frameworks (e.g. net451, netcoreapp1.0).
Now that the official project format is csproj
using MSBuild, how do you specify multiple frameworks to target? I am trying to look for this from the project settings in VS2017, but I am able to only target a single framework from the .NET Core frameworks (it doesn't even list the other full .NET Framework versions which I do have installed):
You can manually edit
.csproj
file for this and setTargetFrameworks
(notTargetFramework
) property.For example see
EFCore.csproj
: https://github.com/aspnet/EntityFrameworkCore/blob/951e4826a38ad5499b9b3ec6645e47c825fa842a/src/EFCore/EFCore.csprojThat is not the project template you want if your library needs to work on multiple platform targets. With this project template, your library can only ever be used in a project that targets .NETCore. The PCL library approach was retired, you now have to pick a .NETStandard.
You do so by starting the project with the "Class Library (.NET Standard)" project template. You now have the option of picking the .NETStandard version. The current compatibility grid is here.
Hopefully they'll keep that linked article updated. This is in flux, .NETStandard 2.0 was nailed down but does not ship yet. Targeted for Q2 of 2017, end of spring probably, it currently shows as 97% done. I overheard the designers saying that using 1.5 or 1.6 is not recommended, not compatible enough with 2.0
You need to manually edit the project file and add s to the default TargetFramework and basically change it to TargetFrameworks. Then you mention the Moniker with a ; separator.
Also you can put the Nuget package references in a conditional ItemGroup manually or using VS Nuget Package Manager.
Here is what your .csproj should look like:
Another workaround I do these days because of missing documentation is that I create a project in VS2015 and form the project.json using the available documentation and intellisense, then open the solution in VS2017 and use the built-in upgrade. I will then look at the csproj file to figure out how to make that configuration happen.
Multi-targeting more esoteric targets without a Moniker:
Microsoft:
If you want to target a Portable Profile it doesn't have a predefined moniker so Portable Profiles also can't infer
TargetFrameworkIdentifier
,TargetFrameworkVersion
, andTargetFrameworkProfile
. Also a compiler constant isn't defined automatically. Finally you have to add all assembly references none are provided by default.This Example below is taken from a project that used the
dynamic
keyword so it additionally needed theMicrosoft.CSharp
assembly, thus you can see how it's references for different targets.I did a beginner's guide to multi-targeting net framework and netcore.
The simplest approach is to get a netcore or netstandard target working first. Then edit the csproj file and go through these steps for the other targets.
<Reference />s
for System.* dlls for any netframework targets just by reading what the build error messages say is missing.<PackageReference />s
in the cases where they are not the same for each target. The easiest trick is to temporarily revert to single targetting so that the GUI will handle the Nuget references correctly for you.