Almost every person has a smartphone and can’t imagine how their lives would be if they would have to go back using the old unbreakable Nokia 1310. And that’s because they can do almost everything since there are thousands of apps. And every day new updates are available for most of the available apps. These updates include bug fixes, security patches, and most importantly, new features. The smartphone users do not hesitate to install these updates knowing that these are meant to help them even more. So why us, the developers, should not do the same when there is a new update for the software framework and for the tools that we use to develop a certain application?
A framework update comes with a lot of good things like the removal of bugs discovered in the previous version(s), patch security flaws, performance improvements, and new features that make the development process easier.
Installing the latest Visual Studio that comes with the latest .NET Framework and .NET Core versions is not enough, some changes need to be done to the projects as well to support the latest features that come with the framework.
This article will be focused on upgrading .NET Framework from version 4.6.1 to 4.7.2 and all the steps to follow to be sure that the application is still running as expected after this change is deployed.
Change the target framework of the projects
In most cases, all projects need to be updated to the desired target framework, but in certain cases, some projects can’t be upgraded (easily) to the desired target framework because it could reference packages that do not have yet support with the latest .NET Framework version.
To update the target framework for all projects, right-click on the project (one by one), click properties, and from the “Application” tab change the target framework to the desired one as in the following screenshot and select “Yes” in the popup that is displayed after the framework is changed.
Packages retargeting
Just setting the .NET Framework version as previously described does not update the target framework of the referenced packages for that corresponding project.
To do this, open the Package Manager Console and run the following command:
PM> Update-Package -Reinstall
This will force the package manager to reinstall every package in every project without changing the version of the referenced package targeting the .NET Framework selected for the corresponding project. Sometimes this does not work as expected and needs to be run manually, this can be achieved by running the following command for each project:
PM> Update-Package -Reinstall -ProjectName Project.Name.Here
Fix possible build errors
After the first two steps are completed you can build your solution (Ctrl + Shift + B) and then run the application to be sure that everything works fine. We encountered some errors that we needed to fix for the solution to be built and ran.
The first error was that a certain package had two versions installed (in our case was System.Net.Http), and that’s because the current version was not compatible with the latest .NET Framework version, hence a newer version was installed. This can easily be fixed by removing the reference to the older version of the package from the Web.config or App.config files, depending on the project type.
The second error that we encountered was that a certain package cannot be found, and the package with the problem was System.Net.Http.Formatting. A bit of googling revealed that this package was discontinued, but now is part of Microsoft.AspNet.WebApi.Client, so installing this package solved the problem.
Depending on the packages used on the project, some other errors might occur, but that can be easily fixed, like the ones above, since other developers certainly ran into these kinds of issues and documented how they managed to fix them. If everything is fine, you can continue with the next step.
Install the latest .NET Framework on the server machine
The latest version of Visual Studio comes with the latest version of the .NET Framework, in our case Visual Studio 2019 and .NET Framework 4.7.2, so the application will run on your machine. But this doesn’t mean that it will also run on the production machine.
Before deploying the updated application to a specific machine, you have to be sure that the latest .NET Framework is installed there too and the application will run as it was running on your development machine. Microsoft created an article solely for this purpose: How to: Determine which .NET Framework versions are installed. If the installed version is an older one, you have to download and install the desired version (in our case .NET Framework 4.7.2) from here. Once this step is completed you can continue to the next step.
Edit CI/CD pipelines
Manually deploying the latest version of the application to the desired environment (test, production) takes time, so we are using Azure Dev Ops pipelines to automate this process (this also takes several minutes, but the developer can to something else in the meantime, like creating a database backup).
All the pipelines that we defined were using Hosted VS2017 as agent pool which failed when building the solution. And that happened because the latest .NET Framework supported by Visual Studio 2017 is .NET Framework 4.7.
Microsoft rolled out a new hosted pool called Azure Pipelines that replaces all the previous hosted pools and includes new ones. We selected this new agent pool, and since the application is running on Windows, we selected windows-2019 as agent specification.
Once this last change is made, run the pipeline, wait for it to successfully run, verify that the application is running as expected on the target environment (do not forget to keep an eye on the logs), and enjoy.
Conclusions
Updating your project solution to the latest framework version takes some time, but it can save time and money in the long run. And this because the latest framework updates include security patches for the holes that hackers and malware know to look for.
On the other hand, the latest framework versions introduce new features, removes outdated features, and fix bugs that cause the application to behave unexpectedly.