Skip to content

Commit 1510dfe

Browse files
#984 single file publish works with Service connectors (#1237)
* 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>
1 parent dc772e3 commit 1510dfe

5 files changed

Lines changed: 38 additions & 14 deletions

File tree

src/Common/src/Common/Reflection/ReflectionHelpers.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,13 @@ private static void TryLoadAssembliesWithAttribute<T>()
304304
where T : AssemblyContainsTypeAttribute
305305
{
306306
var runtimeAssemblies = Directory.GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll");
307-
using var loadContext = new MetadataLoadContext(new PathAssemblyResolver(AllRelevantPaths(runtimeAssemblies, typeof(T))));
307+
var allKnownAssemblyPaths = AllRelevantPaths(runtimeAssemblies, typeof(T));
308+
if (!allKnownAssemblyPaths.Any())
309+
{
310+
return;
311+
}
312+
313+
using var loadContext = new MetadataLoadContext(new PathAssemblyResolver(allKnownAssemblyPaths));
308314
var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies();
309315
var assemblypaths = Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory).Where(f => f.EndsWith("dll", StringComparison.InvariantCultureIgnoreCase));
310316
foreach (var assembly in assemblypaths)
@@ -338,26 +344,20 @@ private static void TryLoadAssembliesWithAttribute<T>()
338344
/// <returns>A list of paths to the runtime, assembly and requested assembly type</returns>
339345
private static List<string> AllRelevantPaths(string[] runtimeAssemblies, Type attributeType)
340346
{
341-
var toReturn = new List<string>(runtimeAssemblies);
342347
var executingAssemblyLocation = Assembly.GetExecutingAssembly().Location;
343348
var typeAssemblyLocation = attributeType.Assembly.Location;
344349
if (string.IsNullOrEmpty(executingAssemblyLocation) || string.IsNullOrEmpty(typeAssemblyLocation))
345350
{
346-
var baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
347-
if (baseDirectory.EndsWith("\\"))
348-
{
349-
baseDirectory = baseDirectory.Substring(0, baseDirectory.Length - 1);
350-
}
351-
352-
toReturn.Add(baseDirectory);
353351
Console.WriteLine("File path path information for the assembly containing {0} is missing. Some Steeltoe functionality may not work with PublishSingleFile=true", attributeType.Name);
352+
return new List<string>();
354353
}
355354
else
356355
{
357-
toReturn.Add(executingAssemblyLocation);
358-
toReturn.Add(attributeType.Assembly.Location);
356+
return new List<string>(runtimeAssemblies)
357+
{
358+
executingAssemblyLocation,
359+
attributeType.Assembly.Location
360+
};
359361
}
360-
361-
return toReturn;
362362
}
363363
}

src/Connectors/src/CloudFoundry/CloudFoundryConnector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Steeltoe.Connector.CloudFoundry;
77
public static class CloudFoundryConnector
88
{
99
/// <summary>
10-
/// Use this method to ensure Steeltoe.Connector.CloudFoundry is loaded
10+
/// Use this method to prevent Steeltoe.Connector.CloudFoundry from being optimized out of the build
1111
/// </summary>
1212
public static void EnsureAssemblyIsLoaded()
1313
{

src/Discovery/src/Consul/Discovery/ConsulDiscoveryClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ internal ConsulDiscoveryOptions Options
3939
}
4040
}
4141

42+
/// <summary>
43+
/// Use this method to prevent Steeltoe.Discovery.Consul from being optimized out of the build
44+
/// </summary>
45+
public static void EnsureAssemblyIsLoaded()
46+
{
47+
// no-op
48+
}
49+
4250
/// <summary>
4351
/// Initializes a new instance of the <see cref="ConsulDiscoveryClient"/> class.
4452
/// </summary>

src/Discovery/src/Eureka/EurekaDiscoveryClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ public EurekaHttpClientInternal(IOptionsMonitor<EurekaClientOptions> config, ILo
3636
private readonly IOptionsMonitor<EurekaClientOptions> _configOptions;
3737
private readonly IServiceInstance _thisInstance;
3838

39+
/// <summary>
40+
/// Use this method to prevent Steeltoe.Discovery.Eureka from being optimized out of the build
41+
/// </summary>
42+
public static void EnsureAssemblyIsLoaded()
43+
{
44+
// no-op
45+
}
46+
3947
public override IEurekaClientConfig ClientConfig => _configOptions.CurrentValue;
4048

4149
public EurekaDiscoveryClient(

src/Discovery/src/Kubernetes/Discovery/KubernetesDiscoveryClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public class KubernetesDiscoveryClient : IDiscoveryClient
2121
private readonly IOptionsMonitor<KubernetesDiscoveryOptions> _discoveryOptions;
2222
private readonly DefaultIsServicePortSecureResolver _isServicePortSecureResolver;
2323

24+
/// <summary>
25+
/// Use this method to prevent Steeltoe.Discovery.Kubernetes from being optimized out of the build
26+
/// </summary>
27+
public static void EnsureAssemblyIsLoaded()
28+
{
29+
// no-op
30+
}
31+
2432
public string Description => "Steeltoe provided Kubernetes native service discovery client";
2533

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

0 commit comments

Comments
 (0)