Problem description
I have a solution based on .NET Framework 4.8, it is a class library project written with VB.NET, built with VS2019 and updated to VS2022.
The project is VERY heavy (a full toolset for .NET developers), thousands of classes, millions of lines of code, etc.
I would like to upgrade this solution to .NET Core 7, but the Upgrade Assistant is a pain to do to this, I just can’t do it in this way due all the compiler errors I get. Anyways, I managed to prune "non-essential" code from the solution generated by the upgrade assistant and fix all compiler errors to be able at least compile the "core" of the code in .NET 7.
However, I have found that this workaround is also quite unfeasible, first because it is missing a lot of code and features from the solution based on .NET FW 4.8, and secondly because I make changes and updates to the solution based on .NET FW 4.8, and then have to copy and adapt these changes and updates to the .NET 7 solution. It is a nightmare for one single person.
Conclusions
So I’ve come to the conclusion that the best option is to create/start a single Visual Studio solution from scratch, in VS2002, a .NET 7 solution configured in such a way that the code can be compiled to build .NET FW 4.8 dlls and .NET 7 dlls, and then "just" I have to gradually copy and adapt the code from the original solution based on .NET FW 4.8, to this new empty solution to make the code compatible with .NET FW 4.8 and .NET 7. And once I finish I can delete forever the solution based on .NET FW 4.8.
I hope you are understanding me correctly at this point.
Question
And what is the problem I have about all what I have exposed above?.
Well, the main problem (aside from the fact that my experience with .NET Core is minimal) is that I’m not very clear on how to do this kind of solution. Or if it can be done.
That is, a .NET 7 solution, with a single class library project, whose configuration is made to compile the code to build .NET FW 4.8 dlls in one directory, and another configuration to compile the same code to build the respective .NET 7 dlls in another directory.
Logically I think I’ll need to use Conditional Compilation so that incompatible code doesn’t compile to build the .NET 7 dlls, but beyond that I don’t quite know how to create the necessary solution configuration.
I guess the question could be summarized as follows:
How to configure a Visual Studio 2022 solution to compile the same code to .NET 4.8 and .NET 7?
I need guidance, advice or examples to start carrying this out.
EDIT:
I just would like to have a single .NET 7 solution on which I can compile the same code to generate dlls for .NET 4.8 (that can be imported in .NET Framework projects) and dlls for .NET 7 (that can be imported in .NET 7 projects), sharing the same code (filtered with conditional compilation), all within the same .NET 7 project / solution.
2
Answers
You can NOT really do this ASSUMING this is a web forms application.
If this is a MVC application, then you can probably convert and run as .NET Core without any much changes.
So, the FIRST answer here is if the project is a webforms project, or if this is a MVC web project?
So, just keep in mind that there is ZERO support for webforms projects using .NET Core. You can’t use or have webforms run as .NET Core right now, and they MUST be .NET Framework, not .NET Core framework.
As noted, if this is a MVC project, then you can use .NET Core, and next to none code changes would be required – with few exceptions.
But, if this is webforms, then you can’t choose .NET Core or .NET Framework. (you must use .NET Framework)
In theory, mvc projects could be based on .NET Core or .NET Framework, but with your given information, this looks to be a webforms project, and not a mvc one, so that being the case, no, you can’t do this.
So, keep in mind that MVC projects are vast different. The difference betwene a MVC project in .NET Core, or a MVC project in .NET Core? You probably can’t tell the difference.
however, webforms vs MVC projects? Now that is a WHOLE different ballgame and matter. Webform projects can’t be converted to MVC projects without a whole re-write of that applcation.
And if the project is a MVC project already? Then I doubt VERY much that one would care or even notice the difference if you using .NET Framework, or .NET Core. And your code behind will be really the same also with few exceptions between .NET Core and .NET frameworks.
There are two main options, the multitargeting + conditional compilation, which you have already (almost) discovered. I was not able to find guidance for VB, but here are docs for C#, maybe they can give you some directions:
Second option which can be considered – using .NET Standard:
So you can move your shared code to .NET Standard library (.NET Standard 2.0 is latest supported by .NET Framework) and this library can be referenced by both .NET (Core) and .NET Framework target projects. Though this option will not be suitable for all kind of libraries, .NET Standard APIs are vast but still quite limited.