From 1074c5e2b349ca936d4046dfdf0b4e2e818ef0dc Mon Sep 17 00:00:00 2001 From: danielmarbach Date: Wed, 30 Aug 2023 18:08:40 +0200 Subject: [PATCH] Encapsulate token extraction into validator --- src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs | 3 +-- .../Hosting/Helpers/AssemblyValidator.cs | 9 ++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs b/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs index 0adb9d17b42..d6128427dd9 100644 --- a/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs +++ b/src/NServiceBus.Core/Hosting/Helpers/AssemblyScanner.cs @@ -374,8 +374,7 @@ bool ShouldScanDependencies(Assembly assembly) return false; } - var tokenString = Convert.ToHexString(assemblyName.GetPublicKeyToken() ?? Array.Empty()); - if (AssemblyValidator.IsRuntimeAssembly(tokenString)) + if (AssemblyValidator.IsRuntimeAssembly(assemblyName)) { return false; } diff --git a/src/NServiceBus.Core/Hosting/Helpers/AssemblyValidator.cs b/src/NServiceBus.Core/Hosting/Helpers/AssemblyValidator.cs index 2407135d315..de0fc3ab63b 100644 --- a/src/NServiceBus.Core/Hosting/Helpers/AssemblyValidator.cs +++ b/src/NServiceBus.Core/Hosting/Helpers/AssemblyValidator.cs @@ -2,6 +2,7 @@ { using System; using System.IO; + using System.Reflection; using System.Reflection.Metadata; using System.Reflection.PortableExecutable; using System.Security.Cryptography; @@ -47,6 +48,12 @@ public static void ValidateAssemblyFile(string assemblyPath, out bool shouldLoad reason = "File is a .NET assembly."; } + public static bool IsRuntimeAssembly(AssemblyName assemblyName) + { + var tokenString = Convert.ToHexString(assemblyName.GetPublicKeyToken() ?? Array.Empty()); + return IsRuntimeAssembly(tokenString); + } + static string GetPublicKeyToken(byte[] publicKey) { using var sha1 = SHA1.Create(); @@ -58,7 +65,7 @@ static string GetPublicKeyToken(byte[] publicKey) return Convert.ToHexString(lastEightBytes); } - public static bool IsRuntimeAssembly(string tokenString) => + static bool IsRuntimeAssembly(string tokenString) => tokenString switch { // Microsoft tokens