core icon indicating copy to clipboard operation
core copied to clipboard

.net core issue, 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)

Open bnlakshmibn opened this issue 8 years ago • 14 comments

.Net core issue with some System reference libraries

Hi, I am trying to create a .net core Web api. I had added my logic there. But while trying to run that, I am getting the exception as 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)

This kind of exception is happening with many "System." assemblies.

bnlakshmibn avatar Mar 06 '18 04:03 bnlakshmibn

Yeah you cannot use dotnet core for WinForms! You have to use FullFramework.

seriouz avatar Mar 07 '18 16:03 seriouz

Looks like you need to figure out what is bringing in the System.Windows.Forms dependency. What is your TargetFramework? What are the other System assemblies?

Petermarcu avatar Mar 10 '18 16:03 Petermarcu

Closing this old issue. Feel free to re-open this with more information.

joshfree avatar Jul 23 '18 18:07 joshfree

I start up my application that throw BadImageFormatException

System.BadImageFormatException: 未能加载文件或程序集“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”或它的某一个依赖项。该模块应包含一个程序集清单。 文件名:“System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”

lindexi avatar Mar 04 '19 07:03 lindexi

i am also facing the same issue when i try to call the .net core 3.0 api to windows workflow foundation. getting following issue. BadImageFormatException: Could not load file or assembly 'System.Activities, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058) Microservices.Controllers.WWFController.Get(int age) in WWFController.cs, line 31

Please help on this

patillaxmi123 avatar Mar 24 '21 12:03 patillaxmi123

@seriouz Hey , i am trying to use winforms for .net Core console app. Is there any documentation for the same that it is not possible for .NET CORE

The same referencing and code works smooth on .NET FRAMEWORK

ChahatKumar avatar May 17 '21 14:05 ChahatKumar

@ChahatKumar Try it with dotnet 5.

seriouz avatar May 17 '21 14:05 seriouz

@ChahatKumar Try it with dotnet 5.

Thanks ! I tried using this but I find that a dotnet 5 console app cannot produce both the form and console window ?right?

ChahatKumar avatar May 17 '21 16:05 ChahatKumar

No i don't think you can run a console app as WinForms app. Why do you need a console app in the first place?

seriouz avatar May 17 '21 17:05 seriouz

I basically have a library written as a console app which I plan to release. But for collection of user data I need a contest frok the user for the same for which I was planning to add a form.

Altho this is possible in a. . Net Framework console app, but I am unsure since a similar procedure didn't work for. NET CORE

My whole library is written in. NET CORE

ChahatKumar avatar May 17 '21 17:05 ChahatKumar

Try this in your csproj. It should keep the console window open of a winforms app.

<PropertyGroup>
  <OutputType>Exe</OutputType>
</PropertyGroup>

seriouz avatar May 17 '21 17:05 seriouz

Hi , it did work .. Thanks ! But now giving an error that a dll is not connected ..

Just wanted to cofirm one thing -> for dll's like System.Windows.Forms -> i referenced them using my internal program files where they are present and added path in .csproj

If i add through COM reference , it gives error and doesn't load ( in case of .net Core app)

I wasnt able to find document for winforms in .NET CORE solving my issue Am i referencing them the right way ?

Thannks and apologies for the out of topic doubt

ChahatKumar avatar May 17 '21 18:05 ChahatKumar

Yes, reference the Packages in your .csproj. Dlls you have to reference like this:

<ItemGroup>
  <Reference Include="YourAssembly">
    <HintPath>Path/To/YourAssembly.dll</HintPath>
  </Reference>
</ItemGroup>

seriouz avatar May 17 '21 19:05 seriouz

Thanks a lot 👍

ChahatKumar avatar May 17 '21 19:05 ChahatKumar