I'm having some issues while playing around with .Net 5, so I'm hoping someone could shed some light on things. Having worked on a large enterprise .Net Framework solution for a number of years, .Net Core seems to have passed me by and I've had no exposure to it, which might explain some of my confusion...
Unit test references?
I have created a solution containing a WPF project ("WPF App (.NET)"), and a unit test project ("MSTest Test project (.Net Core)"). Both have been set to a target framework of ".NET 5" in their properties page.
When I reference the WPF project from the unit test project, a yellow triangle appears alongside the project reference, and the following error in the Error List window:
Project 'MyTestWpf.csproj' targets 'net5.0-windows'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0'
How on earth do I reference the WPF project from the UT project?
Class lib referencing WPF types?
I have also created a .Net 5 class library, which needs to reference various WPF types (controls, etc), but I'm not sure how to configure the necessary assembly/framework references (in the Framework world I'd simply add assembly references to PresentationFramework, System.Xaml, etc). Somehow, through trial and error I did get this working, using a combination of these lines in the project file:-
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<UseWPF>true</UseWPF>
as well as changing the target framework from "net5.0" to "net5.0-windows".
However I'm not sure which combination of these are actually required, or if I'm even supposed to be editing the project file by hand, which feels like a step backwards if it can't be done via Visual Studio!
If your unit test library references your WPF project, the unit tests will also have to target net5.0-windows. @canton7 that did the trick. I think I understand why the tfm needs to be "net5.0-windows", as I guess "net5.0" is cross-platform so wouldn't result in any Windows desktop assemblies being referenced.
Basically, to run unit tests that are WPF related, you can use a class called AvalonTestRunner (in the AvalonUnitTesting.dll supplied with this article) and call the RunInSta method passing a delegate. This will auto magically switch the current thread apartment to STA for you…
Project 'MyTestWpf.csproj' targets 'net5.0-windows'. It cannot be referenced by a project that targets '.NETCoreApp,Version=v5.0' How on earth do I reference the WPF project from the UT project? Class lib referencing WPF types?
This code base is a fork of the WPF code in the .NET Framework. . NET Core 3.0 was released with a goal of WPF having parity with the .NET Framework version. Over time, the two implementations may diverge.
Since .NET 5.0, creating new project for WPF and Windows Forms is different from those in .NET Core 3.0 and 3.1.
Since .NET 5.0 and later, creating new project for WPF and Windows Forms requires Target Framework name with OS specific, and the SDK header in the project file of supported languages such as csproj (C#), vbproj (VB), fsproj (F#) are using the same SDK moniker like those for class library and console apps.
Therefore in .NET 5.0 and later you don't have to specify Sdk="Microsoft.NET.Sdk.WindowsDesktop"
for any projects that use WPF and Windows Forms, but you must specify explicit <UseWPF>
and <UseWindowsForms>
as needed.
For example, this is a basic WinForms executable project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows7.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
For unit test project that has reference to Windows Forms, you can update your unit test project like this example: (notice the additional <UseWindowsForms>true</UseWindowsForms>
element)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.4" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.4" />
<PackageReference Include="coverlet.collector" Version="1.3.0" />
</ItemGroup>
</Project>
@canton really has the correct answer here, but it's a bit lost in the comments. To clarify, by default your UnitTest project is probably targeting net5.0
and it needs to target net5.0-windows
.
You can just modify by hand the UnitTest.csproj
file to make this change.
<PropertyGroup>
<TargetFramework>net5.0-windows</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With