I have a small library that currently supports .NET 2.0+.
I don't use any features of the later framework versions so it would be nice to keep 2.0 support, but I also want to target .NET Core (or more precisely, .NET Standard).
I tried to add both frameworks to project.json
:
"frameworks": {
"net20": {},
"netstandard1.6": {
"imports": "dnxcore50"
}
}
But the NuGet packages my library needs to run on .NET Standard (System.Reflection
and Microsoft.AspNetCore.WebUtilities
) aren't compatible with .NET 2.0.
How can I solve this issue without maintaining two completely separate projects with almost identical code?
You can't, if you depend on
Microsoft.AspNetCore.*
packages as the absolute minimum for supporting .NET Standard is .NET 4.5..NET 4.5 is the first version to include
System.Runtime
on which .NET Core is based on. But when you think closer about it, it also makes no sense. If you need support for ASP.NET Core within your library.If your library is supposed to run for ASP.NET Core and ASP.NET 4 (i.e. MVC 5, WebApi 2), then you will need to use your ASP.NET Dependencies conditionally and use
#if
preprocessor directives.I used
netstandard1.3
as that's the minimum forMicrosoft.AspNetCore.WebUtilities
, but depending on your other dependencies you may need to go higher or lower.NameOf.AspNetLegacyPackage
is the name of the package which contains the same functionality asMicrosoft.AspNetCore.WebUtilities
which you need, but which works on .NET Framework 2.0, if there is any. If not you have to remove it and write the replacement functions yourself.Then in your code use
Alternatively, if you are going to give up the .NET Framework 2.0 support and go for 4.5.1, you can keep using
Microsoft.AspNetCore.WebUtillities
(see NuGet page for dependencies) it in both