question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forms dll not found when using Forms in a dependent DLL referenced by a project without Microsoft.NET.Sdk.WindowsDesktop

See original GitHub issue

We 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:open
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dsplaistedcommented, Apr 10, 2020

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.

0reactions
Meccuzcommented, Apr 10, 2020

Do you have any news on this issue? Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NET project SDK overview
A project that references a project SDK is sometimes referred to as an ... NET Desktop SDK, which includes Windows Forms (WinForms) and ......
Read more >
Use Windows Forms in a .Net Core Class Library
If for some reason you are looking for dlls, you can find them in C:\Program Files\dotnet\packs\Microsoft.WindowsDesktop.App.Ref\3.0.0-preview6- ...
Read more >
.NET core vs .NET framework
Wondering about the difference between .NET Core & .NET Framework? Here's a quick guide on how to pick the right runtime environment for...
Read more >
Solution to Visual Studio 2022 messing up ...
NET Standard 2 projects couldn't be loaded anymore in Visual Studio 2019. The error shown when attempting to load the projects was:.
Read more >
Resolved - Can not reference System.WindowsForms in Net5
I'm converting to Net5. I have 40 out of 41 projects converted OK The remaining project will not let me reference System.WindowsForms.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found