Skip to content

Commit

Permalink
#984 single file publish works with Service connectors (#1237)
Browse files Browse the repository at this point in the history
* changed handling for published single file to silently fail not loading additional dll for #984
* add/update assembly-loading helpers for PublishSingleFile scenarios

---------

Co-authored-by: Tim Hess <thess@vmware.com>
  • Loading branch information
thompson-tomo and TimHess authored Jan 24, 2024
1 parent dc772e3 commit 1510dfe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 14 deletions.
26 changes: 13 additions & 13 deletions src/Common/src/Common/Reflection/ReflectionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,13 @@ private static void TryLoadAssembliesWithAttribute<T>()
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)
Expand Down Expand Up @@ -338,26 +344,20 @@ private static void TryLoadAssembliesWithAttribute<T>()
/// <returns>A list of paths to the runtime, assembly and requested assembly type</returns>
private static List<string> AllRelevantPaths(string[] runtimeAssemblies, Type attributeType)
{
var toReturn = new List<string>(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<string>();
}
else
{
toReturn.Add(executingAssemblyLocation);
toReturn.Add(attributeType.Assembly.Location);
return new List<string>(runtimeAssemblies)
{
executingAssemblyLocation,
attributeType.Assembly.Location
};
}

return toReturn;
}
}
2 changes: 1 addition & 1 deletion src/Connectors/src/CloudFoundry/CloudFoundryConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Steeltoe.Connector.CloudFoundry;
public static class CloudFoundryConnector
{
/// <summary>
/// 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
/// </summary>
public static void EnsureAssemblyIsLoaded()
{
Expand Down
8 changes: 8 additions & 0 deletions src/Discovery/src/Consul/Discovery/ConsulDiscoveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ internal ConsulDiscoveryOptions Options
}
}

/// <summary>
/// Use this method to prevent Steeltoe.Discovery.Consul from being optimized out of the build
/// </summary>
public static void EnsureAssemblyIsLoaded()
{
// no-op
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsulDiscoveryClient"/> class.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/Discovery/src/Eureka/EurekaDiscoveryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public EurekaHttpClientInternal(IOptionsMonitor<EurekaClientOptions> config, ILo
private readonly IOptionsMonitor<EurekaClientOptions> _configOptions;
private readonly IServiceInstance _thisInstance;

/// <summary>
/// Use this method to prevent Steeltoe.Discovery.Eureka from being optimized out of the build
/// </summary>
public static void EnsureAssemblyIsLoaded()
{
// no-op
}

public override IEurekaClientConfig ClientConfig => _configOptions.CurrentValue;

public EurekaDiscoveryClient(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ public class KubernetesDiscoveryClient : IDiscoveryClient
private readonly IOptionsMonitor<KubernetesDiscoveryOptions> _discoveryOptions;
private readonly DefaultIsServicePortSecureResolver _isServicePortSecureResolver;

/// <summary>
/// Use this method to prevent Steeltoe.Discovery.Kubernetes from being optimized out of the build
/// </summary>
public static void EnsureAssemblyIsLoaded()
{
// no-op
}

public string Description => "Steeltoe provided Kubernetes native service discovery client";

public IList<string> Services => GetServices(null);
Expand Down

0 comments on commit 1510dfe

Please sign in to comment.