Forms dll not found when using Forms in a dependent DLL referenced by a project without Microsoft.NET.Sdk.WindowsDesktop
See original GitHub issueWe have some .NET Core applications that reference 3rd party assemblies (initially made for .NET Classic) that occasionally open a custom message box and therefore references Windows Forms.
Now that .NET Core 3.0 supports Windows Forms we can finally use those assemblies in .NET Core applications who themselves do not use Windows Forms at all.
However currently (SDK 3.0.100-preview-010184), the main application csproj that references this 3rd party assembly must be set to <Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
event though it is just a simple console app and has no knowledge of the fact that one of its dependencies uses windows forms. If I do not set the SDK as above, I get the following exception:
System.IO.FileNotFoundException HResult=0x80070002 Message=Could not load file or assembly ‘System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’. The system cannot find the file specified. Source=winformslib StackTrace: at winforms.PublicApi.Show() in C:\temp\winforms\winformslib\Form1.cs:line 32 at app.Program.Main(String[] args) in C:\temp\winforms\app\Program.cs:line 11
Here is the code that can be used to reproduce the issue:
app.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\winformslib\winformslib.csproj" />
</ItemGroup>
</Project>
Program.cs
using System;
using winforms;
namespace app
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("I will now open a windos form");
PublicApi.Show();
Console.WriteLine("It should be open now");
Console.ReadLine();
}
}
}
winformslib.csproj
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace winforms
{
internal partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
private void buttonExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
public static class PublicApi
{
public static void Show()
{
var form = new Form1();
form.Show();
}
}
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
If you have a dependency coming in via a NuGet package reference or a project reference that targets .NET Core and uses Windows Forms, then the dependency on Windows Forms should flow to your app and it should just work.
If you depend on a .NET Framework library that depends on Windows Forms, then it doesn’t express that dependency in the same way, and you will have to explicitly refer to Windows Forms in your main app. We don’t currently plan to change this.
Do you have any news on this issue? Thank you.