Repository navigation
Use DependencyContext to load diagnostics assembly - #958
Conversation
adamsitnik
left a comment
There was a problem hiding this comment.
@wojtpl2 big thanks for help!
could you please answer my question before I merge it?
|
|
||
| private static Assembly LoadDiagnosticsAssembly(Assembly benchmarkDotNetCoreAssembly) | ||
| { | ||
| // We couldn't use Assembly.GetEntryAssembly()?.GetReferencedAssemblies() because |
There was a problem hiding this comment.
I think that we should keep the old way (to make it work for old .NET frameworks) and extend it with the new one.
So it would be:
var referencedAssemblyName = Assembly.GetEntryAssembly()?.GetReferencedAssemblies().SingleOrDefault(name => name.Name == DiagnosticAssemblyName); if (referencedAssemblyName != default(AssemblyName))
return Assembly.Load(referencedAssemblyName);
var inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(l => l.Name.Equals(DiagnosticAssemblyName, StringComparison.OrdinalIgnoreCase));
var inRuntimeLibraries = DependencyContext.Default.RuntimeLibraries.Any(l => l.Name.Equals(DiagnosticAssemblyName, StringComparison.OrdinalIgnoreCase));
if (inCompileLibraries || inRuntimeLibraries)
return Assembly.Load(new AssemblyName(DiagnosticAssemblyName));There was a problem hiding this comment.
I'll check old .Net frameworks path.
There was a problem hiding this comment.
Agreed. DependencyContext doesn't work "by default" on .NET Framework. DependencyContext is mostly just for the .NET Core case.
You should also check for DependencyContext.Default == null, which will be the case when a .deps.json file isn't present.
|
|
||
| if (inCompileLibraries || inRuntimeLibraries) | ||
| { | ||
| return Assembly.Load(new AssemblyName(DiagnosticAssemblyName)); |
There was a problem hiding this comment.
I would assume that the DependencyContext exposes some method to load the library without providing the name. Here you just check that it's on the list and if it is you load it by name. How Assembly.Load will know about the .deps file here?
@eerhardt how should we use DependencyContext to load an assembly?
There was a problem hiding this comment.
I found this way there https://github.com/dotnet/corefx/issues/11639#issuecomment-300565009.
I created project on the side and this solution works.
I don't know how Assembly.Load works. I'll try to check.
There was a problem hiding this comment.
DependencyContext allows you to get the information about a dependency, but it doesn't have assembly loading capabilities. You still use the normal Assembly.Load APIs to load the assembly.
The reason I suggested using DependencyContext is because I assumed the main thing you need/want here is you want to get the AssemblyVersion of the referenced BenchmarkDotNet.Diagnostics.Windows.dll, right? (That information can be found on this API).
If it works to just Assembly.Load(new AssemblyName(DiagnosticAssemblyName));, then you don't need the DependencyContext nor the bit about Assembly.GetEntryAssembly()?.GetReferencedAssemblies() at all. Just call Assembly.Load passing in the .dll name.
There was a problem hiding this comment.
Calling Assembly.Load is enough for .net core and full framework.
For full framework, it works even if instead of referencing a library, I copy the dll to the bin directory.
| if (referencedAssemblyName != default(AssemblyName)) | ||
| return Assembly.Load(referencedAssemblyName); | ||
| // So we use DependencyContext that reads *.deps.json file that contains all referenced assembly. | ||
| var inCompileLibraries = DependencyContext.Default.CompileLibraries.Any(l => l.Name.Equals(DiagnosticAssemblyName, StringComparison.OrdinalIgnoreCase)); |
There was a problem hiding this comment.
You shouldn't check the CompileLibraries at all. These are not appropriate for runtime scenarios, and are mainly there to support things like ASP.NET MVC Razor, which compiles code at runtime using Roslyn.
|
@eerhardt @adamsitnik Big thanks for this review. It is amazing how much I can learn in this project. I'll fix this PR.. |
|
As @eerhardt wrote, BDN doesn't need DependencyContext at all. I tested this solution for .net core and full framework. |
|
@wojtpl2, unfortunately, it needs it. The difference is when you work with our local project built on your PC (when everything is in bin) vs BDN installed via NuGet (when BDN is in NuGet folder sth like C:\Users\adsitnik.nuget\packages) The problem that we try to solve is the following:
Repro: class Program
{
static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
public class Sample
{
[Benchmark] public void TheMethod() { }
}Reproducing this issue is not easy. The best way to do it know to me:
Once you are able to repro the problem locally and provide a fix you need to remove the local NuGet from |
|
I check your path, and I have to say that it works. @eerhardt explained exactly why it works.:
|
|
Without changes I got exception: After changes: |
|
@wojtpl2 Great! btw I am an idiot. There is much simpler way of testing it: just create new project, install BDN packages and copy-paste the code for loading the dlls to the new app and run it. |
|
You are not an idiot ;) Thanks to you I started my journey with open source. |
|
Thanks, @wojtpl2, for the great work here! Fixing this issue will help quite a few customers, especially ML.NET, where this issue was spawned from. |
|
@wojtpl2 I just tested it myself and everything works fine. Once again thanks for help! |

Fix for #955