Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve file loading issues originating from Runtime store libraries #3343

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/OpenTelemetry.AutoInstrumentation.Loader/Loader.Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal partial class Loader
{
internal static System.Runtime.Loader.AssemblyLoadContext DependencyLoadContext { get; } = new ManagedProfilerAssemblyLoadContext();

internal static string[]? StoreFiles { get; } = GetStoreFiles();

private static string ResolveManagedProfilerDirectory()
{
string tracerFrameworkDirectory = "net";
Expand All @@ -21,6 +23,29 @@ private static string ResolveManagedProfilerDirectory()
return Path.Combine(tracerHomeDirectory, tracerFrameworkDirectory);
}

private static string[]? GetStoreFiles()
{
try
{
var storeDirectory = Environment.GetEnvironmentVariable("DOTNET_SHARED_STORE");
if (storeDirectory == null || !Directory.Exists(storeDirectory))
{
return null;
}

var architecture = Environment.Is64BitProcess ? "x64" : "x86";
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
var targetFramework = $"net{Environment.Version.Major}.{Environment.Version.Minor}";
var finalPath = Path.Combine(storeDirectory, architecture, targetFramework);

var storeFiles = Directory.GetFiles(finalPath, "Microsoft.Extensions*.dll", SearchOption.AllDirectories);
pjanotti marked this conversation as resolved.
Show resolved Hide resolved
return storeFiles;
}
catch
{
return null;
}
}

private static Assembly? AssemblyResolve_ManagedProfilerDependencies(object? sender, ResolveEventArgs args)
{
var assemblyName = new AssemblyName(args.Name);
Expand Down Expand Up @@ -56,8 +81,16 @@ private static string ResolveManagedProfilerDirectory()
Logger.Debug("Loading {0} with DependencyLoadContext.LoadFromAssemblyPath", path);
return DependencyLoadContext.LoadFromAssemblyPath(path); // Load unresolved framework and third-party dependencies into a custom Assembly Load Context
}
else
{
var entry = StoreFiles?.FirstOrDefault(e => e.EndsWith($"{assemblyName.Name}.dll"));
if (entry != null)
{
return DependencyLoadContext.LoadFromAssemblyPath(entry);
}

return null;
return null;
}
}
}
#endif
Loading