11

I have some dll that is written in .Net framework 4.0 and I can't run my program when I'm referencing it to my project which is written in .NET core 2.0.

Although my IDE (vs 2017) can recognize the objects imported from that dll correctly in run time Im having the following exception:

System.BadImageFormatException: 'Could not load file or assembly 'A_dotnet_4.0_A, Version=10.0.0.0, Culture=neutral, PublicKeyToken=0ad20d08c672086a'. An attempt was made to load a program with an incorrect format.'

I tried to:

  1. change my settings to any CPU as I saw in a post here
  2. tried to clean-rebuild my project.

Is it even possible? and if it does, how should I do so. In the following link is seem like it is possible - I just can't understand how.

Green
  • 2,405
  • 3
  • 22
  • 46
  • 2
    See [this table](https://learn.microsoft.com/en-us/dotnet/standard/net-standard#net-implementation-support) for version compatibility – Camilo Terevinto Oct 08 '18 at 13:26

3 Answers3

13

You cannot do this.

.NET Core can reference a .NET Standard DLL
.NET Framework can reference a .NET Standard DLL

.NET Core cannot reference a .NET Framework DLL (or visa versa).

If you have for example a .NET Standard Project, you cannot reference .NET Framework and the .NET Core framework.
It's one or the other.

enter image description here

To further elaborate on this, we have a project that has shared BusinessLogic, that project is a .NET Standard 2.0 Library.
We reference that project in 2 other projects a.NET Core 2.1 and a .NET Framework 4.7.

Things go wrong when you reference .NET Core or .NET Framework items directly to that shared .NET Standard library.

mybrave
  • 1,662
  • 3
  • 20
  • 37
Hypenate
  • 1,907
  • 3
  • 22
  • 38
  • 7
    "NET Core cannot reference a .NET Framework DLL (or visa versa)." doesn't appear to be true. According to the Github link Green posted, you can reference a .NET Framework dll from a .NET Core project. AFAIK it needs to be a .NET Framework version that implements .NET Standard, so it needs to be .NET Framework 4.5 and up. – Bas Oct 08 '18 at 13:21
  • 2
    Second comment in his link: "This is something we're looking to make possible in the upcoming update to VS. The plan is to let you reference the dll but depending on the dll, it may or may not work depending on what it depends on." – Hypenate Oct 08 '18 at 13:25
  • 1
    Fifth comment: "This is now possible from a .NET Core 2.0 project." which is what Green is using. – Bas Oct 08 '18 at 13:25
  • 2
    @Hypenate That was in **May 2017**. Look at the comment on **Oct 6, 2017**: "This is now possible from a .NET Core 2.0 project. I am going to close this. It's expected to work so if you hit any issues, please open a new issue with the specific details." – Camilo Terevinto Oct 08 '18 at 13:25
  • He does not explicitly state that it's possible with anything you depend on... So I'm not too sure. – Hypenate Oct 08 '18 at 13:28
  • How can you have a .NET Standard project at work? .NET Standard is just the standard, not an implementation. – Bas Oct 08 '18 at 13:37
  • `(or visa versa)` this is simply wrong. It only works in one direction. – nvoigt Oct 08 '18 at 13:37
  • 1
    @nvoigt Correct! My bad. – Hypenate Oct 09 '18 at 09:08
  • 1
    This answer is incorrect. Here is the correct answer: https://stackoverflow.com/a/62051508/3595459 The feature first added in .NET Core 2.0 https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-2-0#support-for-net-framework-libraries – Kosta_Arnorsky Sep 24 '20 at 18:49
0

Is it even possible?

No, it's not.

  • .NET Core can run assemblies targeting either .NET Core itself or .NET Standard
  • .NET Framework can run assemblies targeting either .NET Framework itself or .NET Standard

So there is no way to bring two assemblies that target Full Framework and Core respectively together. Not in Core and not in full framework.

If you need a shared library that both can access, that would need to target .NET standard to be able to be used by both full framework and Core.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 2
    .NET Framework 4.5 and up implements.NET Standard, so that works. – Bas Oct 08 '18 at 13:47
  • @Bas a framework cannot target something, only a build output can. – nvoigt Oct 08 '18 at 13:47
  • Noted, I meant "implements" rather than "targets". – Bas Oct 08 '18 at 13:48
  • That the framework implements something means it can run it. Yes, full framework can run .NET Standard. But when my build output targets full framework, Core cannot run it. – nvoigt Oct 08 '18 at 13:49
  • 2
    And yet, a .NET Core 2.0 build can reference a .NET Framework 4.5 dll. – Bas Oct 08 '18 at 13:50
  • 2
    This answer is incorrect. Here is the correct answer: https://stackoverflow.com/a/62051508/3595459 The feature first added in .NET Core 2.0 https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-2-0#support-for-net-framework-libraries – Kosta_Arnorsky Sep 24 '20 at 18:49
-1

In summary:

It doesn't work because the .NET Framework version of that DLL is too low. .NET Core 2.0 and up support referencing other .NET Standard dlls (so either .NET Core or .NET Framework, or whichever other future library might implement .NET Standard).

.NET Framework 4.0 does not implement .NET Standard, so it does not produce .NET Standard compatible dlls: .NET Framework only started implementing .NET Standard from version 4.5 and up. So if you can find a version of that DLL compiled for .NET Framework 4.5, it should theoretically work, barring some edge cases.

Bas
  • 1,946
  • 21
  • 38
  • thanks on the help, was an inserting discussion no doubt - I would also add the table which @CamiloTerevinto mentioned for documentation – Green Oct 08 '18 at 13:33
  • 1
    No, he can use a .NET 4.5 library. He doesn't need to use a dll that implements the same version of .NET Standard that the .NET Core project implements, it just needs to implement .NET Standard, period. The .NET Standard versions are backwards compatible, so a lower version of .NET Standard will still work. – Bas Oct 08 '18 at 13:34
  • While "it does not work" is correct, your solution would not work either. .NET Core and .NET Full Framework *cannot* reference each other, they can only reference heir own framework and .NET Standard projects. – nvoigt Oct 08 '18 at 13:36
  • @Bas Supposing the library only uses .NET Standard 1.0-available APIs, yes, you are correct. – Camilo Terevinto Oct 08 '18 at 13:37
  • A .NET Framework 4.5 project -is- a .NET Standard project. – Bas Oct 08 '18 at 13:38
  • @Bas No? It's not. – nvoigt Oct 08 '18 at 13:38
  • 1
    @Bas No, it's not. A .NET Framework project is a .NET Framework project. A .NET Standard DLL can be used in Linux. A .NET Framework DLL cannot – Camilo Terevinto Oct 08 '18 at 13:39
  • Yes. it is. It is -specifically- mentioned in the table you linked to. – Bas Oct 08 '18 at 13:40
  • No, it's not. It says ".NET Framework **implements** .NET Standard**, not that it is. – Camilo Terevinto Oct 08 '18 at 13:40
  • Bas, you don't need to believe people on the internet. Just *try* what you think will work. When it doesn't, read the documentation again. – nvoigt Oct 08 '18 at 13:41
  • nvoigt, why don't you try it? Create a .NET Framework 4.5 class library and a .NET Core 2.1 console app. Reference the class library from the console app, build, and run. Notice how it works. – Bas Oct 08 '18 at 13:46
  • Notice how what works? An empty example? Try `MessageBox.Show("Hello World");` and see how it fails. – nvoigt Oct 08 '18 at 13:58
  • That's the point, it does not fail. It builds and executes. Try defining some classes in the .NET Framework project and instantiating and working with them in the .NET Core project. It builds and executes just fine. – Bas Oct 08 '18 at 14:07
  • You start a .NET Core console project (on Linux for example) and get a Windows Forms MessageBox? Could you attach a screenshot? – nvoigt Oct 08 '18 at 14:10
  • Now you're moving goalposts. The question was wether a .NET Framework assembly can be referenced and used in a .NET Core project, not wether Windows Forms controls can run on Linux. – Bas Oct 08 '18 at 14:12
  • 1
    Well, if your claim is "very specifically selected full framework assemblies may on some systems be referenced and run in .NET Core" then Ill say... maybe? I don't care. If your claim is "Full Framework always produces assemblies that can always be run under .NET Core" then that's wrong. And Windows Forms was the easiest example to show that. – nvoigt Oct 08 '18 at 14:15
  • That wasn't my claim, so I'm not sure where this entire line of reasoning is going. – Bas Oct 08 '18 at 14:16
  • By the way, what do you think a ":Net Standard" library is good for, if a full framework assembly does what you claim it does. – nvoigt Oct 08 '18 at 14:16
  • I don't think you are understanding what I'm claiming is possible, based on the examples you are giving. – Bas Oct 08 '18 at 14:17
  • Anyway, I'm tired of this, and the site is suggesting we move this to chat, which is something I'm not interested in. Green has the correct answer he needed, and has moved on. So should we. – Bas Oct 08 '18 at 14:19
  • This answer is incorrect. Here is the correct answer: https://stackoverflow.com/a/62051508/3595459. TL;DR: in principle it's possible, but with many limitations, the primary of which is the compatibility of API used by .NET Framework library with .NET Core. The feature first added in .NET Core 2.0 https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-2-0#support-for-net-framework-libraries Up to my knowledge it has nothing to do with .NET Standard. – Kosta_Arnorsky Sep 24 '20 at 18:38