Upgrading to .Net 8 results in errors when referenced by a project targeting '.NetFramework=4.61'

Ziya Ganjili 20 Reputation points
2024-04-29T22:40:37.7233333+00:00

I am using the upgrade assistant to upgrade the framework, but it's resulting in many errors. Is there any documentation available that explains step-by-step how to fix the errors that occur during the framework upgrade process?

.NET Training
.NET Training
.NET: Microsoft Technologies based on the .NET software framework.Training: Instruction to develop new skills.
15 questions
{count} votes

Accepted answer
  1. Michael Taylor 55,676 Reputation points
    2024-04-30T02:08:21.9+00:00

    NET Framework 4.x is a completely different runtime than NET Core (5/6/7/8). They are not compatible at runtime. Therefore you cannot reference a NET 5/6/7/8 project in a NET Framework assembly. That is the error you're getting.

    If you are in the middle of running the upgrade assistant then generally you should run it on the projects with the least dependencies first and then work your way up. Once you upgrade a project then any project that hasn't been upgraded yet will generate this error most likely. You can ignore it (and therefore not be able to compile your code) until you upgrade the later project.

    If you need to support multiple frameworks in the same project at the same time then you need to multi-target your project. To do that open the project file in the editor and change from <TargetFramework> to <TargetFrameworks>. Then specify each supported framework sesparated by semicolons. For example the following allows your project to support both .NET framework 4.7.2 and NET 8.

    <TargetFrameworks>net472;net8.0</TargetFrameworks>
    

    Now your project can be referenced by either NET 4.7.2 or NET 8 projects. A caveat to this however is that your project is compiled twice, once for each framework. Therefore your code has to support both frameworks. You cannot, for example, use C# features, like implicit usings, in your project because it isn't supported by NET 4.7.2. So you may either need to conditionally compile your code (using C# preprocessor directive) or avoid any features/types not supported by both frameworks. Additionally if you rely on any NuGet packages then they also need to support the frameworks. Sometimes you may run across a package that either requires 2 different versions (one for each framework) or doesn't work at all. In that case you have to conditionally reference the package. For example NET Framework can use System.ComponentModel.DataAnnotations from the framework but the NET8.0 version has to use NuGet. It is tedious but hopefully only temporary while you migrate.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

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.