Use version of the assembly with the same name as the package ID (#16544)

This commit is contained in:
Ronald Barendse
2024-09-26 07:51:16 +02:00
committed by GitHub
parent 609b5f76d4
commit 14a0e62278

View File

@@ -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 class PackagingService : IPackagingService
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 class PackagingService : IPackagingService
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;
}
}