Porting Windows Forms to .Net Standard 2.0

12

Apologies if this is a very naive question. I wrote a Windows Form application using .Net 4.5 sometime ago. Recently, I thought it would be a good idea to port it to a .Net Standard 2.0 application using VS Code.

There were a couple of problems with missing libraries and classes (System.ServiceModel being the biggest gap), but I have got to the point of building the application successfully. However, when I come to run it, I see the following error:

Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058) ---> System.BadImageFormatException: Cannot load a reference assembly for execution.

Here's the project file if it's useful:

<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.8" />
<PackageReference Include="Microsoft.CSharp" Version="4.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NUnit" Version="3.8.1" />
<PackageReference Include="System.CodeDom" Version="4.4.0" />
<PackageReference Include="System.Configuration" Version="2.0.5" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.4.0" />
<PackageReference Include="System.Net.Http" Version="4.3.3" />
<PackageReference Include="System.ServiceModel" Version="1.0.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.0" />
<PackageReference Include="System.Windows.Forms" Version="4.0.0.0" />
</ItemGroup>

<PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

</Project>

Is there some way to run the application using the System.Windows.Forms library, or should I replace it with a different library for .Net Standard 2.0?

c#
.net
winforms
.net-standard-2.0
asked on Stack Overflow Nov 5, 2017 by user304582 • edited Dec 12, 2020 by Drew Noakes

1 Answer

11

You cannot build an application that targets .NET Standard 2.0. Only libraries can target .NET Standard. Applications still have to target a specific runtime - .NET Framework, .NET Core, Xamarin, etc.

Judging by your project file, you're actually targeting .NET Core 2.0.

System.Windows.Forms is part of the .NET Framework (and Mono). It does not exist as part of .NET Standard or .NET Core.

Your project file has a PackageReference to System.Windows.Forms, which will pull in this NuGet package. It's unofficial and unlisted. It's very description is "unsupported library".

The DLL inside that package is only a reference assembly, i.e. none of the methods contain any actual code. That assembly only contains definitions.

This is what the error message means by "Reference assemblies should not be loaded for execution." The assembly in that package will let you compile, but nothing else.

There is no formal way to run a Windows Forms application on .NET Core (prior to 3.0). There may be benefit to pulling our your business logic into a .NET Standard assembly, but your user interface will still have to be a .NET Framework application.

answered on Stack Overflow Nov 5, 2017 by yaakov • edited Nov 7, 2019 by yaakov

User contributions licensed under CC BY-SA 3.0