Add option to prefer .NET Standard over .NET Framework as TargetFramework #641
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds the option to prioritize .NET Standard over .NET Framework as TargetFramework.
Background
As written in Issue #621, the currently supported Unity differs from the original .NET Framework, possessing an API surface of .NET Standard 2.1, even when the API Compatibility Level is set to
.NET Framework.Due to Unity's .NET Framework a strange and unique combination of .NET Framework 4.8 and .NET Standard 2.1, various issues arise when an assembly targeted at
net46xis installed.For example,
Microsoft.Bcl.AsyncInterfacesfornet462(.NET Framework) includesSystem.IAsyncDisposable, which is provided as an in-built API in .NET Standard 2.1. As a result, installing it in a .NET Framework Unity project causes the following compile error:Additionally,
Grpc.Net.Clienthas libraries fornet461that are not compatible with .NET Standard 2.x and do not work, leading to unnecessary dependencies being installed.To address this issue, #630 made it possible to specify
targetFrameworkin the packages.config, but this does not affect transitive dependencies. In some cases, you need to specify this for each implicitly installed package. (Example: Our project's installation of Grpc.Net.Client in package.json)Hence, I added an option to always prioritize .NET Standard 2.x over .NET Framework as the target framework choice in the project.
Remarks
The option added by this PR is turned off by default, but in my opinion, making this behavior of prioritizing .NET Standard the default could be appropriate.The option added by this PR is turned off by default. This means that the behaviour does not change in existing projects. But it turned on by default when NuGet.config is newly created.
I believe that libraries built for .NET Standard 2.x, especially 2.1, are more modern than those built specifically for the .NET Framework. In Unity, if
API Compatibility Levelchanges from.NET Standardto.NET Framework, the legacy API set could be used instead of the libraries that could be used in .NET Standard 2.1, but I don't think any developer would like that.