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

Improve the dynamic loading of Diagnostics package #955

Closed
adamsitnik opened this Issue Nov 13, 2018 · 4 comments

Comments

Projects
None yet
3 participants
@adamsitnik
Member

adamsitnik commented Nov 13, 2018

We have BenchmarkDotNet.Diagnostics.Windows.dll and BenchmarkDotNet.dll. BenchmarkDotNet.Diagnostics.Windows.dll has dependency to BenchmarkDotNet.dll, not the opposite.

When the user specifies --profiler ETW we parse command line arguments in BenchmarkDotNet.dll and we try to load BenchmarkDotNet.Diagnostics.Windows.dll in a dynamic way using Assembly.Load and others:

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);

@eerhardt how can we load it without <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> in NuGet friendly way?

@eerhardt

This comment has been minimized.

Member

eerhardt commented Nov 13, 2018

This is exactly the scenario that DependencyContext is meant to solve.

You can find all the dependencies of the current application by looking in DependencyContext.Default.RuntimeLibraries.

Note this typically only works on .NET Core projects, which automatically create .deps.json files. It will work on .NET Framework projects, if the application is built to produce a .deps.json file (not the default on .NET Framework).

@wojtpl2

This comment has been minimized.

Collaborator

wojtpl2 commented Nov 14, 2018

I did this issue. I'll push PR in no time.

@wojtpl2

This comment has been minimized.

Collaborator

wojtpl2 commented Nov 14, 2018

I created PR #958.

@adamsitnik adamsitnik added this to the v0.11.3 milestone Nov 14, 2018

@wojtpl2

This comment has been minimized.

Collaborator

wojtpl2 commented Nov 14, 2018

@adamsitnik Do you have any other task that I can take care of? I can finish #912 if you want. Of course, I'll keep your history of changes. Or I can take something else.

@adamsitnik adamsitnik changed the title from How to dynamically load BenchmarkDotNet.Diagnostics.Windows without NuGet hacks to Improve the dynamic loading of Diagnostics package Nov 16, 2018

@adamsitnik adamsitnik removed the discussion label Nov 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment