
A while ago, I had to migrate a large legacy .NET Framework website to the latest 4.8 version. Recently, I had to do this again for a number of smaller projects, so I had to look up my notes again on how to do that.
Maybe someone out there will still find some use in these notes, so here’s how I approached that migration.
1. Set the framework version
Mine was a web project, so I had to update my web.config files as follows. For other projects, it’ll be in your app.config.
In system.web
set the targetframework
to 4.8.
<httpRuntime targetFramework="4.8"/>
<compilation debug="true" targetFramework="4.8" defaultLanguage="c#"/>
In the csproj
files of your project, you of course have to also set the target framework version.You can do this manually using Visual Studio and click away like a zombie for an hour and bore yourself senseless. I wrote a PowerShell script to change this for me. I had way too many projects to update for that kind of manual grind. Besides, writing a script is way more fun than clicking away in Visual Studio.
This is what the end result of the chance has to be in your project file:
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
The script below does this for you, from a target version, to the requested version, for all .csproj files in the current folder and all subfolders.
Super handy.
<#
.SYNOPSIS
Change .NET TargetFrameworkVersion property in csproj files.
.PARAMETER FromVersion
From-version to look for and change, e.g. 4.5.1
.PARAMETER ToVersion
To-version to set, e.g. 4.8
.EXAMPLE
upgrade-framework.ps1 4.5.1 4.8
#>
param ($FromVersion, $ToVersion)
function Replace-FileContent (
[string][Parameter(mandatory=$true)]
$path,
[string][Parameter(mandatory=$true)]
$matchContent,
[string][Parameter(mandatory=$true)]
$replaceContent)
{
Write-Host "Processing file $path"
$content = Get-Content -path $path -encoding UTF8
$content = $content | % { $_ -replace $matchContent, $replaceContent }
$content | Set-Content $path -encoding UTF8
}
Write-Host "Searching $FromVersion projects..."
$f = ls *.csproj -r | sls "targetframeworkversion" | where { $_.line -like "*v$FromVersion*" }
Write-Host "Change the framework version on the projects to $ToVersion"
$f.path | % { Replace-FileContent -path $_ -matchContent "<TargetFrameworkVersion>v$FromVersion</TargetFrameworkVersion>" -replaceContent "<TargetFrameworkVersion>v$ToVersion</TargetFrameworkVersion>" }
2. Update all the packages
Alright, now we’ve set the framework version to what it should be. We also have to update all the NuGet dependencies for all projects, so they use the 4.8 version of the package, if available.
You can do this by running this command in the Package Manager Console in Visual Studio.
Update-Package -Reinstall
Just sit back and watch it do its magic. Afterward, you’ll notice your packages.config
files will reference the 4.8 version of your packages. There are also some other ways to do this, listed here, but I found this to be the quickest.
3. Upgrade your target and build machines
This sort of speaks for itself, but if you want to build and deploy this anywhere, you’ll have to install .NET Framework 4.8 on those machines as well.
Luckily, the DevOps guys automated all this using Ansible for me, so I only had to pass them the correct Chocolatey packages to install.
I’m not 100% sure about this anymore, but I think I used these.
For the build server:
choco install netfx-4.8-devpack
For the runtime on the servers:
choco install netfx-4.8.1
Tips & tricks
Now it’s down to the hard part. Testing everything, and seeing if the upgrade broke anything. I can’t help much with that, but here are some things I ran into, which might also help in your upgrade quest.
- I had service reference files in the project, and for some reason the package upgrade command added the .dll extension to those files. This had to be reverted.
- It created a bunch of
ReactConfig.cs
files, which I didn’t need, so I deleted them. - Some packages were not using the same version over all projects, like
Newtonsoft.Json
. I fixed this by setting them all to the same version, by runningUpdate-Package Newtonsoft.Json -Version 11.0.1
Another thing that came in handy, was this PowerShell snippet to find out what Framework version is installed on a machine. Handy to see if your deployment target, or build server, actually have the right Framework version installed.
(Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full").Version
Happy upgrading!
The post how to upgrade to .NET framework 4.8 appeared first on n3wjack's blog.