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

Use DependencyContext to load diagnostics assembly #958

Merged
merged 2 commits into from
Nov 15, 2018
Merged
Changes from all commits
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
26 changes: 4 additions & 22 deletions src/BenchmarkDotNet/Diagnosers/DiagnosersLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,16 @@ private static IDiagnoser[] LoadClassic()
{
try
{
var benchmarkDotNetCoreAssembly = typeof(DefaultConfig).GetTypeInfo().Assembly;
var benchmarkDotNetAssembly = typeof(DefaultConfig).GetTypeInfo().Assembly;

var diagnosticsAssembly = LoadDiagnosticsAssembly(benchmarkDotNetCoreAssembly);
var diagnosticsAssembly = Assembly.Load(new AssemblyName(DiagnosticAssemblyName));

if (diagnosticsAssembly.GetName().Version != benchmarkDotNetCoreAssembly.GetName().Version)
if (diagnosticsAssembly.GetName().Version != benchmarkDotNetAssembly.GetName().Version)
{
string errorMsg =
$"Unable to load: {DiagnosticAssemblyFileName} version {diagnosticsAssembly.GetName().Version}" +
Environment.NewLine +
$"Does not match: {Path.GetFileName(benchmarkDotNetCoreAssembly.Location)} version {benchmarkDotNetCoreAssembly.GetName().Version}";
$"Does not match: {Path.GetFileName(benchmarkDotNetAssembly.Location)} version {benchmarkDotNetAssembly.GetName().Version}";
ConsoleLogger.Default.WriteLineError(errorMsg);
}
else
Expand All @@ -94,24 +94,6 @@ private static IDiagnoser[] LoadClassic()
};
}

private static Assembly LoadDiagnosticsAssembly(Assembly benchmarkDotNetCoreAssembly)
{
// it not enough to just install NuGet to be "referenced", the project has to consume the dll for real to be on the referenced assembly list
var referencedAssemblyName = Assembly.GetEntryAssembly()?.GetReferencedAssemblies().SingleOrDefault(name => name.Name == DiagnosticAssemblyName);
if (referencedAssemblyName != default(AssemblyName))
return Assembly.Load(referencedAssemblyName);

// we use the location of BenchmarkDotNet.dll, because it should be in the same folder
string directoryName = new FileInfo(benchmarkDotNetCoreAssembly.Location).DirectoryName
?? throw new DirectoryNotFoundException(benchmarkDotNetCoreAssembly.Location);
string diagnosticAssemblyBinPath = Path.Combine(directoryName, DiagnosticAssemblyFileName);
if (File.Exists(diagnosticAssemblyBinPath))
return Assembly.LoadFile(diagnosticAssemblyBinPath);

// Assembly.LoadFrom(fileName) searches in current directory, not bin, but it's our last chance
return Assembly.LoadFrom(DiagnosticAssemblyFileName);
}

private static IDiagnoser CreateDiagnoser(Assembly loadedAssembly, string typeName)
=> (IDiagnoser)Activator.CreateInstance(loadedAssembly.GetType(typeName));
}
Expand Down