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.

1
  • 2
    See this table for version compatibility Commented Oct 8, 2018 at 13:26

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

"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.
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."
Fifth comment: "This is now possible from a .NET Core 2.0 project." which is what Green is using.
@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."
@nvoigt Correct! My bad.
|
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.

6 Comments

.NET Framework 4.5 and up implements.NET Standard, so that works.
@Bas a framework cannot target something, only a build output can.
Noted, I meant "implements" rather than "targets".
And yet, a .NET Core 2.0 build can reference a .NET Framework 4.5 dll.
This answer is incorrect. Here is the correct answer: stackoverflow.com/a/62051508/3595459 The feature first added in .NET Core 2.0 learn.microsoft.com/en-us/dotnet/core/whats-new/…
|
-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.

21 Comments

thanks on the help, was an inserting discussion no doubt - I would also add the table which @CamiloTerevinto mentioned for documentation
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.
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.
@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
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.