Standard: Can I import net472 project from a .netstandard project? Also, should I?

Created on 11 Dec 2019  路  3Comments  路  Source: dotnet/standard

Imagine the following scenario:

A------------------->B--------------------->C
(Net472)----------->(NetStandard)--------->(Net472)

Main project A in NET framework, referencing a .Net Standard project B which references a NET Framework 472 project.

In theory, as Net Standard is a subset of NET framework, it shouldn't make sense that I can reference the whole from within a subset right? However, if I create a simple App with that structure I'm able to build and run it without errors or warnings.

BUT, if I try this in other applications, mainly a big one that I'm porting to .NET Standard, I get warning in the .NET Standard projects referencing .NET 472. When is this warning raised after all?

C.csproj was resolved using '.NETFramework,Version=v4.7.2' instead of the project target framework '.NETStandard,Version=v2.0'. This project may not be fully compatible with your project.

And, can I or can't I reference a NET472 project from within a .NET Standard 2.0/2.1?

Most helpful comment

When B uses C, there is a chance that C uses .NET Framework-specific functionality that are not supported on .NET Standard 2.0 where B is on. A warning is raised.
When A uses B, as B is a .NET Standard 2.0 library and .NET Framework implements it, it doesn't raise a warning. It does not go down the dependency chain to check every project to avoid unnecessary warnings.

All 3 comments

Imagine this:

A | B | C
-|-|-
Xamarin | .NET Standard | .NET Framework

where C uses Windows-specific functionality like System.Drawing.
When A uses C, the whole application will blow up.

This warning is there to remind users of this scenario.

But there are a lot of existing libraries (accumulated over 15 years!) that depend on .NET Framework which are unmaintained. Most of them use the .NET Standard subset of .NET Framework, allowing them to be safely used from .NET Standard. This is a huge quality of life bonus when there were not as many .NET Standard libraries out there. This benefit outweighs the above scenario, so you can use .NET Framework libraries from .NET Standard.

@Happypig375 Thanks a lot for the answer.

So, do you know why some of my projects are raising these warnings, while others are not?

When B uses C, there is a chance that C uses .NET Framework-specific functionality that are not supported on .NET Standard 2.0 where B is on. A warning is raised.
When A uses B, as B is a .NET Standard 2.0 library and .NET Framework implements it, it doesn't raise a warning. It does not go down the dependency chain to check every project to avoid unnecessary warnings.

Was this page helpful?
0 / 5 - 0 ratings