diff --git a/src/Common/src/Common/Reflection/ReflectionHelpers.cs b/src/Common/src/Common/Reflection/ReflectionHelpers.cs index 74eefcdcd8..47f17a6083 100644 --- a/src/Common/src/Common/Reflection/ReflectionHelpers.cs +++ b/src/Common/src/Common/Reflection/ReflectionHelpers.cs @@ -304,7 +304,13 @@ private static void TryLoadAssembliesWithAttribute() where T : AssemblyContainsTypeAttribute { var runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll"); - using var loadContext = new MetadataLoadContext(new PathAssemblyResolver(AllRelevantPaths(runtimeAssemblies, typeof(T)))); + var allKnownAssemblyPaths = AllRelevantPaths(runtimeAssemblies, typeof(T)); + if (!allKnownAssemblyPaths.Any()) + { + return; + } + + using var loadContext = new MetadataLoadContext(new PathAssemblyResolver(allKnownAssemblyPaths)); var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); var assemblypaths = Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory).Where(f => f.EndsWith("dll", StringComparison.InvariantCultureIgnoreCase)); foreach (var assembly in assemblypaths) @@ -338,26 +344,20 @@ private static void TryLoadAssembliesWithAttribute() /// A list of paths to the runtime, assembly and requested assembly type private static List AllRelevantPaths(string[] runtimeAssemblies, Type attributeType) { - var toReturn = new List(runtimeAssemblies); var executingAssemblyLocation = Assembly.GetExecutingAssembly().Location; var typeAssemblyLocation = attributeType.Assembly.Location; if (string.IsNullOrEmpty(executingAssemblyLocation) || string.IsNullOrEmpty(typeAssemblyLocation)) { - var baseDirectory = AppDomain.CurrentDomain.BaseDirectory; - if (baseDirectory.EndsWith("\\")) - { - baseDirectory = baseDirectory.Substring(0, baseDirectory.Length - 1); - } - - toReturn.Add(baseDirectory); Console.WriteLine("File path path information for the assembly containing {0} is missing. Some Steeltoe functionality may not work with PublishSingleFile=true", attributeType.Name); + return new List(); } else { - toReturn.Add(executingAssemblyLocation); - toReturn.Add(attributeType.Assembly.Location); + return new List(runtimeAssemblies) + { + executingAssemblyLocation, + attributeType.Assembly.Location + }; } - - return toReturn; } } \ No newline at end of file diff --git a/src/Connectors/src/CloudFoundry/CloudFoundryConnector.cs b/src/Connectors/src/CloudFoundry/CloudFoundryConnector.cs index 5e61c52732..23ff37c2af 100644 --- a/src/Connectors/src/CloudFoundry/CloudFoundryConnector.cs +++ b/src/Connectors/src/CloudFoundry/CloudFoundryConnector.cs @@ -7,7 +7,7 @@ namespace Steeltoe.Connector.CloudFoundry; public static class CloudFoundryConnector { /// - /// Use this method to ensure Steeltoe.Connector.CloudFoundry is loaded + /// Use this method to prevent Steeltoe.Connector.CloudFoundry from being optimized out of the build /// public static void EnsureAssemblyIsLoaded() { diff --git a/src/Discovery/src/Consul/Discovery/ConsulDiscoveryClient.cs b/src/Discovery/src/Consul/Discovery/ConsulDiscoveryClient.cs index 67ab4bda75..964434dbc7 100644 --- a/src/Discovery/src/Consul/Discovery/ConsulDiscoveryClient.cs +++ b/src/Discovery/src/Consul/Discovery/ConsulDiscoveryClient.cs @@ -39,6 +39,14 @@ internal ConsulDiscoveryOptions Options } } + /// + /// Use this method to prevent Steeltoe.Discovery.Consul from being optimized out of the build + /// + public static void EnsureAssemblyIsLoaded() + { + // no-op + } + /// /// Initializes a new instance of the class. /// diff --git a/src/Discovery/src/Eureka/EurekaDiscoveryClient.cs b/src/Discovery/src/Eureka/EurekaDiscoveryClient.cs index be1705c194..38d686aa2f 100644 --- a/src/Discovery/src/Eureka/EurekaDiscoveryClient.cs +++ b/src/Discovery/src/Eureka/EurekaDiscoveryClient.cs @@ -36,6 +36,14 @@ public EurekaHttpClientInternal(IOptionsMonitor config, ILo private readonly IOptionsMonitor _configOptions; private readonly IServiceInstance _thisInstance; + /// + /// Use this method to prevent Steeltoe.Discovery.Eureka from being optimized out of the build + /// + public static void EnsureAssemblyIsLoaded() + { + // no-op + } + public override IEurekaClientConfig ClientConfig => _configOptions.CurrentValue; public EurekaDiscoveryClient( diff --git a/src/Discovery/src/Kubernetes/Discovery/KubernetesDiscoveryClient.cs b/src/Discovery/src/Kubernetes/Discovery/KubernetesDiscoveryClient.cs index e337cfface..ee19151727 100644 --- a/src/Discovery/src/Kubernetes/Discovery/KubernetesDiscoveryClient.cs +++ b/src/Discovery/src/Kubernetes/Discovery/KubernetesDiscoveryClient.cs @@ -21,6 +21,14 @@ public class KubernetesDiscoveryClient : IDiscoveryClient private readonly IOptionsMonitor _discoveryOptions; private readonly DefaultIsServicePortSecureResolver _isServicePortSecureResolver; + /// + /// Use this method to prevent Steeltoe.Discovery.Kubernetes from being optimized out of the build + /// + public static void EnsureAssemblyIsLoaded() + { + // no-op + } + public string Description => "Steeltoe provided Kubernetes native service discovery client"; public IList Services => GetServices(null);