referencing .NET framework 4 dll in .NET core 2.0

12,845

Solution 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.

Solution 2

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.

Share:
12,845

Related videos on Youtube

Green
Author by

Green

Updated on September 15, 2022

Comments

  • Green
    Green over 1 year

    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.

    • Camilo Terevinto
      Camilo Terevinto over 5 years
      See this table for version compatibility
  • Bas
    Bas over 5 years
    "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.
  • Hypenate
    Hypenate over 5 years
    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."
  • Bas
    Bas over 5 years
    Fifth comment: "This is now possible from a .NET Core 2.0 project." which is what Green is using.
  • Camilo Terevinto
    Camilo Terevinto over 5 years
    @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."
  • Hypenate
    Hypenate over 5 years
    He does not explicitly state that it's possible with anything you depend on... So I'm not too sure.
  • Green
    Green over 5 years
    thanks on the help, was an inserting discussion no doubt - I would also add the table which @CamiloTerevinto mentioned for documentation
  • Bas
    Bas over 5 years
    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.
  • nvoigt
    nvoigt over 5 years
    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
    Bas over 5 years
    How can you have a .NET Standard project at work? .NET Standard is just the standard, not an implementation.
  • nvoigt
    nvoigt over 5 years
    (or visa versa) this is simply wrong. It only works in one direction.
  • Camilo Terevinto
    Camilo Terevinto over 5 years
    @Bas Supposing the library only uses .NET Standard 1.0-available APIs, yes, you are correct.
  • Bas
    Bas over 5 years
    A .NET Framework 4.5 project -is- a .NET Standard project.
  • nvoigt
    nvoigt over 5 years
    @Bas No? It's not.
  • Camilo Terevinto
    Camilo Terevinto over 5 years
    @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
  • Bas
    Bas over 5 years
    Yes. it is. It is -specifically- mentioned in the table you linked to.
  • Camilo Terevinto
    Camilo Terevinto over 5 years
    No, it's not. It says ".NET Framework implements .NET Standard**, not that it is.
  • nvoigt
    nvoigt over 5 years
    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.
  • Bas
    Bas over 5 years
    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
    Bas over 5 years
    .NET Framework 4.5 and up implements.NET Standard, so that works.
  • Bas
    Bas over 5 years
    And yet, a .NET Core 2.0 build can reference a .NET Framework 4.5 dll.
  • nvoigt
    nvoigt over 5 years
    Notice how what works? An empty example? Try MessageBox.Show("Hello World"); and see how it fails.
  • Bas
    Bas over 5 years
    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.
  • nvoigt
    nvoigt over 5 years
    You start a .NET Core console project (on Linux for example) and get a Windows Forms MessageBox? Could you attach a screenshot?
  • Bas
    Bas over 5 years
    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.
  • nvoigt
    nvoigt over 5 years
    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.
  • Hypenate
    Hypenate over 5 years
    @nvoigt Correct! My bad.
  • Kosta_Arnorsky
    Kosta_Arnorsky over 3 years
    This answer is incorrect. Here is the correct answer: stackoverflow.com/a/62051508/3595459 The feature first added in .NET Core 2.0 docs.microsoft.com/en-us/dotnet/core/whats-new/…
  • Kosta_Arnorsky
    Kosta_Arnorsky over 3 years
    This answer is incorrect. Here is the correct answer: stackoverflow.com/a/62051508/3595459 The feature first added in .NET Core 2.0 docs.microsoft.com/en-us/dotnet/core/whats-new/…