.Net 7 project use and call .Net Framework 4.6 project - failed

Eli7 21 Reputation points
2022-12-28T14:10:25.867+00:00

We have a .Net Framework 4.6.1 project that includes services using Entity Framework 6 and reading the connection string from the app config file.
We included it on a .Net 7 web API, so we have a project reference to the old project (that has been mentioned above).
When I ran the Web API project (.net 7) it failed with the exception of missing DLLs etc.
the last one is: "TypeLoadException: Could not load type 'System.Runtime.CompilerServices.Closure' from assembly 'System.Core' "

To solve the issue I installed the following NuGet packages on the API project:

  • Microsoft.Windows.Compatibility
  • EntityFramework (version 6.0)
  • System.Configuration.ConfigurationManager
  • System.Data.SqlClient.

They all do not help.

I am wondering if maybe there is a way to do it - to reference a .Net Framework 4.6.1 project and call it from a .Net 7 project.

I will be happy to hear your guidance!
thanks!

Developer technologies C#
{count} votes

Accepted answer
  1. Lex Li (Microsoft) 6,037 Reputation points Microsoft Employee
    2022-12-31T08:34:42.63+00:00

    The only feasible way I can think of is to turn your .NET Framework 4.6.1 class library project as a multi-targeting one, like I wrote in "Tips on Target Platform Selection". and in your case, simply target both net7.0 and net461 as those are required by your projects.

    .NET Standard 2.0 is no longer a suitable target to choose as it is too limited (.NET 5/6/7 expanded the API surface significantly).

    The benefits of switching to multi-targeting is that the C# compiler immediately informs you of incompatible code at compile time, so you can use conditional compilation to exclude certain pieces (such as usage of EF 6) from net7.0. That's much better than blindly loading the assembly by .NET 7 and receiving miserable runtime exceptions.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-12-28T16:58:19.537+00:00

    Unfortunately there is no .netstandard 2.0 support for EF 6. Either convert the 4.6.1 project to core, or if you need to share with a 4.* application, make it a multi-target 4.6.1 and netstandard 2.1 or net 7


  2. PatriceSc 171 Reputation points
    2022-12-29T15:20:54.37+00:00

    Hi,

    You had .NET Framework 1.x to 4.x on one side (Windows only) and then a full cross platform rewrite as .NET Core 1.x to 3.x followed by .NET 5.x or later (dropped "Core" as you never had a .NET Framework 5.x which should avoid any confusion).

    So when you create a DLL either you target a particular framework and have access to all of its capabilities or you can target ".NET Standard" which allows to run on all frameworks from both "families" that are making available this particular API subset (see the link already posted).

    Now it depends if what you want to share is compatible with ".NET Standard 2.0". A possible approach is then move over time compatible code to its own DLL that will be consumed by both your current .NET 4.6 dll or directly by your new .NET 7.x projects.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.