The problem
Let’s say you have a ASP.NET Core application without the bundled ASP.NET Core runtime (e.g. to keep the download as small as possible) and you want to run your ASP.NET Core application on a Windows Server hosted by IIS.
General approach
The general approach is the following: Install the .NET Core hosting bundle and you are done.
Each .NET Core Runtime (and there are quite a bunch of them) is backward compatible (at least the 2.X runtimes), so if you have installed 2.2.6, your app (created while using the .NET runtime 2.2.1), still runs.
Why check the minimum version?
Well… in theory the app itself (at least for .NET Core 2.X applications) may run under runtime versions, but each version might fix something and to keep things safe it is a good idea to enforce security updates.
Check for minimum requirement
I stumbled upon this Stackoverflow question/answer and enhanced the script, because that version only tells you “ASP.NET Core seems to be installed”. My enhanced version searchs for a minimum required version and if this is not installed, it exit the script.
$DotNetCoreMinimumRuntimeVersion = [System.Version]::Parse("2.2.5.0")
$DotNETCoreUpdatesPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Updates\.NET Core"
$DotNetCoreItems = Get-Item -ErrorAction Stop -Path $DotNETCoreUpdatesPath
$MinimumDotNetCoreRuntimeInstalled = $False
$DotNetCoreItems.GetSubKeyNames() | Where { $_ -Match "Microsoft .NET Core.*Windows Server Hosting" } | ForEach-Object {
$registryKeyPath = Get-Item -Path "$DotNETCoreUpdatesPath\$_"
$dotNetCoreRuntimeVersion = $registryKeyPath.GetValue("PackageVersion")
$dotNetCoreRuntimeVersionCompare = [System.Version]::Parse($dotNetCoreRuntimeVersion)
if($dotNetCoreRuntimeVersionCompare -ge $DotNetCoreMinimumRuntimeVersion) {
Write-Host "The host has installed the following .NET Core Runtime: $_ (MinimumVersion requirement: $DotNetCoreMinimumRuntimeVersion)"
$MinimumDotNetCoreRuntimeInstalled = $True
}
}
if ($MinimumDotNetCoreRuntimeInstalled -eq $False) {
Write-host ".NET Core Runtime (MiniumVersion $DotNetCoreMinimumRuntimeVersion) is required." -foreground Red
exit
}
The “most” interesting part is the first line, where we set the minimum required version.
If you have installed a version of the .NET Core runtime on Windows, this information will end up in the registry like this:
Now we just need to compare the installed version with the existing version and know if we are good to go.
Hope this helps!