I’m migrating several projects from .NET Framework to .NET Standard 2.0. As part of this migration, I need to ensure that all project references assemblies that are compatible with .NET Standard. However, I’ve encountered an issue where references to non-.NET Standard assemblies produce runtime exceptions but there aren’t any compile-time warnings or errors.
Let’s look at one example. Li1.csproj
is written in .net framework 4.8 and is not compatible with .netstandard2.0.
There is a compile-time warning in case of a ProjectReference
<ProjectReference Include="Lib1" />
NU1702 ProjectReference 'Lib1.csproj' was resolved using '.NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.0'. This project may not be fully compatible with your project.
If we use an assembly reference, there aren’t any compile errors but a runtime error is raised that the assembly cannot be loaded.
<Reference Include="Lib1">
<HintPath>..Lib1bin$(Configuration)Lib1.dll</HintPath>
</Reference>
How can I configure my .NET Standard project to produce a compile-time error if a referenced assembly is not compatible with .NET Standard? Is there a way to automatically enforce this check across all projects during the build process?
I’ve tried using some common tools like Microsoft.CodeAnalysis.NetAnalyzers and ApiPort, but they don’t seem to address this specific issue.
1