Use version of the assembly with the same name as the package ID (#16544)
(cherry picked from commit 14a0e62278)
This commit is contained in:
committed by
Bjarke Berg
parent
a76af1de9d
commit
635d9b83f9
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user