Edit: Thanks to @JohnSkeet for pointing out a easier Solution:
Replace in the csproj of your Net Standard libary the TargetFramework with TargetFrameworks
<TargetFrameworks>net461;netstandard2.1</TargetFrameworks>
Info: Net461 is the first netFramework version supporting Net Standard 2.0 libraries.
If you need net framework or netcore specific references you can as he pointed out use MS Build Actions in your csproj file
https://github.com/googleapis/gax-dotnet/blob/8c5682ad53f5d93716df5ab762c6ce4790edad72/Google.Api.Gax/Google.Api.Gax.csproj#L25
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System.Net.Http" />
</ItemGroup>
The only reason below to use my old solution if you are dead set that you only want to use the Visual Studio UI and not modify the csproj with a texteditor for custom references or NuGetPackages.
Thanks Greetings Michael
Old Solution
Hello I think I found a solution to my Problem:
- Create a Shared Project Named MyLibrary.Api
(A Shared project since a .NET Standard 2.0 and a .NET Standard 2.1 Project in the same folder cause errors https://github.com/dotnet/sdk/issues/2720)
Also the Shared Project should have the name of the root namespace, since Shared Projects can't have default namespaces and all new files get the name of the project as namespace.
Add a .Net Standard Class Library MyLibrary.Api.Core and Change its Target to to Net Standard 2.1 in (For .NET Core Projects)
Reference MyLibary.Api under Add-Reference->Shared Projects, now at compiliation all Files of the Shared Project get compiled into MyLibrary.Api.Core.
Add a .Net Standard Class Library MyLibrary.Api.NetFramework, Leave it at Target Net Standard 2.0 (This is for Net Framework Projects
- Also add a refererence to to MyLibrary.Api Shared Project.
- Add the latest Version of Microsoft.Net.Compilers NuGet Package to the NetFramework Project
For testing:
- Add a NetCore Console Project MyLibrary.Console.Core and reference the MyLibrary.Api.Core.
- Add a netFramework Console Project MyLibrary.Console.NetFramework and reference MyLibrary.Api.netFramework. Set the .NET Framework Version to at least 4.6.1 because lower versions do not support .Net Standard 2.0 Projects.
Now Activate C# 8.0
Now in all Projects in the .csproj File (Except the MyLibrary.Api Shared Project) add to the first PropertyGroup LangVersion and Nullable (Nullable is not nessary it just turns on nullable types by default, if you don't add it you need to add in Every code file with #nullable enabled):
<PropertyGroup>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
...
In the .NET Framework Project I added a new PropertyGroup Section with LangVersion but I think that is not nessary.
Hope this also helps others.
My Solution at people who want to use NetStandard 2.1 Advantages for their .net core projects but still want to use a version of the library in .NET Framework and can even use nullable types in .NET Framework.
Also this way you compile two dlls. One for NetFramework and one for .NET Core in NetStandard 2.1