diff --git a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs index c4e152e0b5a9..53d6c8ba6c09 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/PackagingService.cs @@ -1,3 +1,6 @@ +using System.Diagnostics.CodeAnalysis; +using System.Reflection; +using System.Runtime.Loader; using System.Xml.Linq; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; @@ -354,8 +357,16 @@ public async Task> GetAllInstalledPackagesAsync() if (!string.IsNullOrEmpty(packageManifest.Version)) { + // Always use package version from manifest installedPackage.Version = packageManifest.Version; } + else if (string.IsNullOrEmpty(installedPackage.Version) && + string.IsNullOrEmpty(installedPackage.PackageId) is false && + TryGetAssemblyInformationalVersion(installedPackage.PackageId, out string? version)) + { + // Use version of the assembly with the same name as the package ID + installedPackage.Version = version; + } } // Return all packages with an ID or name in the package manifest or package migrations @@ -414,4 +425,20 @@ public Task> GetInstalledPackagesFromMigrationPlans return packageFile.CreateReadStream(); } + + private static bool TryGetAssemblyInformationalVersion(string name, [NotNullWhen(true)] out string? version) + { + foreach (Assembly assembly in AssemblyLoadContext.Default.Assemblies) + { + AssemblyName assemblyName = assembly.GetName(); + if (string.Equals(assemblyName.Name, name, StringComparison.OrdinalIgnoreCase) && + assembly.TryGetInformationalVersion(out version)) + { + return true; + } + } + + version = null; + return false; + } }