7

I have a scenario where we are using a legacy .Net Framework dll in .Net core 3.1 class library. Internally .Net Framework dll is using System.Windows.Forms.dll which .Net core is not able to resolve.

I am getting below error message during runtime

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.

In a ideal world there is no point in adding System.Windows.Forms.dll to a class library but is there any way to add System.Windows.Forms and its dependencies in .Net core class library.

Note: I have tried manually adding System.Windows.Forms.dll but it did not work out.

Baala Srinivas K
  • 339
  • 1
  • 3
  • 14
  • 1
    What do you mean "not able to resolve"? Can you edit your post to include the exact error message that you are seeing? – John Wu Jan 24 '20 at 05:01

2 Answers2

17

I got the resolution, we can achieve this by framework reference.

<FrameworkReference Include="Microsoft.WindowsDesktop.App" /> 

For more details please refer https://natemcmaster.com/blog/2019/01/09/netcore-primitives-3/

Baala Srinivas K
  • 339
  • 1
  • 3
  • 14
  • I tried this approach with a .NET Core 3.1 Azure Function and it didn't help. – Murphybro2 Nov 18 '20 at 11:52
  • @Murphybro2 had you ever resolved this? If so, what was the solution? I am also facing problems with .NET Core 2.1 / 3.1 Azure functions – Jurgen Cuschieri Jan 20 '21 at 10:46
  • @JurgenCuschieri No unfortunately not. We decided it wasn't possible and had to change the spec. We were also trying to use a .dll in an Azure function that required Windows Forms. – Murphybro2 Jan 20 '21 at 13:18
  • 2
    If you want to use winforms dll in .net core 3.0.5 then you need to change is as below in your csproj file. net5.0-windows True – Jivan Bhandari May 03 '21 at 04:52
1

It works with me in .net core 3.1 by adding <UseWindowsForms>True</UseWindowsForms> inside project .csproj file to become like this:

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>True</UseWindowsForms>
</PropertyGroup>
Hosam.Yousof
  • 107
  • 9