NU1201: xUnit project not compatible with .Net 5 WinForms project

I have a .Net 5 WinForms Control library (Windows 10, Visual Studio 2019) and want to add some unit tests for the non-UI classes. After adding an xUnit project and referencing my library I get this error:

error NU1201: Project WinFormsControlLibrary1 is not compatible with net5.0 (.NETCoreApp,Version=v5.0). Project WinFormsControlLibrary1 supports: net5.0-windows7.0 (.NETCoreApp,Version=v5.0)

This happens because the WinForms library targets .Net 5 Windows, which is more specific than general .Net 5.

I fix this by double-clicking the unit test project (in solution explorer) and editing the target framework from:

<TargetFramework>net5.0</TargetFramework>

To:

<TargetFramework>net5.0-windows</TargetFramework>

Then, close my project and Visual Studio, and re-open it. (This gives VS2019 a chance to figure what’s going on!) Now the project compiles and I can create and run unit tests.


Note: The Target Framework, shown in the project properties page, is now blank:

So I select .Net 5, close the page, and look again at the project JSON file – now the target framework has changed to:

<TargetFramework>net5.0-windows7.0</TargetFramework>

What’s the difference? I think, nothing! Although at some point I’m sure this will change. More information here: https://github.com/dotnet/sdk/issues/14553