How to handle assemblies that fail to resolve now there is no DomainContext.AssemblyResolve event?
Hi,
In the full .NET Framework I can subscribe to the AppDomain.AssemblyResolve event and handle the discovery of assemblies that have failed to load.
If .NET Core fails to find an assembly how can this be handled? There's a Resolving event available via AssemblyLoadContext.Default but this isn't really what I'm after. I thought the following code might do the the job but of course "var assembly = assemblyLoadContext.LoadFromAssemblyName(assemblyName);" results in a cyclic loop.
Could you please advise how this can now be achieved?
AssemblyLoadContext.Default.Resolving += this.OnAssemblyResolve;
protected virtual Assembly OnAssemblyResolve(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
{
var assembly = assemblyLoadContext.LoadFromAssemblyName(assemblyName);
if (assembly == null)
{
assembly = this.HandleDiscovery(assemblyName);
}
return assembly;
}
Would anyone know?
Could someone please provide an answer here? I'm guessing that no answer means that this very important capability has been overlooked in dotnetcore?
Does anyone know?
I am also interested in an answer on this topic.
does anyone know the answer?
@gkhanna79 Any pointers for AssemblyLoadContext usage?
Resolving event is the thing you want and is equivalent to what AppDomain.ResolveAssembly is about.
"var assembly = assemblyLoadContext.LoadFromAssemblyName(assemblyName);" results in a cyclic loop.
This happens because you are attempting to load an assembly whose dependencies are not yet loaded and thus, since they cannot be found, we invoke the Resolving event again. This behaviour is what the Desktop would also demonstrate.
CC @rahku
This happens because you are attempting to load an assembly whose dependencies are not yet loaded and thus, since they cannot be found, we invoke the Resolving event again.
This would be the expected behaviour if further calls to the 'Resolving' event were attempting to load those not-yet-loaded dependencies. Unfortunately what seems to be happening is that the 'Resolving' event is called with the exact same AssembyName parameter, and not it's dependencies, causing the infinite loop.
Can you please share a repro for this?
Also, can you please move this issue to CoreCLR repo?
CC @rahku
@patricknolan Below is a hack workaround for this issue. It's not pretty, but it works:
private static readonly object _lock = new object();
AssemblyLoadContext.Default.Resolving += this.OnAssemblyResolve;
protected virtual Assembly OnAssemblyResolve(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
{
lock (_lock)
{
AssemblyLoadContext.Default.Resolving -= this.OnAssemblyResolve;
var assembly = assemblyLoadContext.LoadFromAssemblyName(assemblyName);
AssemblyLoadContext.Default.Resolving += this.OnAssemblyResolve;
if (assembly == null)
{
assembly = this.HandleDiscovery(assemblyName);
}
return assembly;
}
}
You should also want to put a try-catch on this to make sure that the event is re-added if there is an exception.
To understand this better, please share a repro for the recursion you are running into.
The issue can be reproduced with the following (as shown my @patricknolan ):
AssemblyLoadContext.Default.Resolving += this.OnAssemblyResolve;
AssemblyLoadContext.Default.LoadFromAssemblyName(AssemblyThatCannotBeFoundByDefault);
protected virtual Assembly OnAssemblyResolve(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
{
var assembly = assemblyLoadContext.LoadFromAssemblyName(assemblyName);
if (assembly == null)
{
assembly = this.HandleDiscovery(assemblyName);
}
return assembly;
}
The issue as far as I can see is due to the following scenario:
When AssemblyLoadContext fails to load an assembly (after calling Assembly.Load behind the scenes), the logic back to the Resolving event. If the Resolving event tries to fall-back to default resolving logic and in-turn calls the AssemblyLoadContext Load methods for the same AssemblyName, it will fail to load again, calling the Resolving event again, creating the infinite loop.
In the above code, the call assemblyLoadContext.LoadFromAssemblyName(assemblyName) will result in the infinite loop, since the fact that the event is being called, means the default logic failed to load the assembly. There is no infinite-loop detection logic in the assembly loading process.
It should probably be documented that the Resolving event is called only after the default assmebly loading fails (and not when it succeeds). It is not a way to override default assembly discovery.
There is no infinite-loop detection logic in the assembly loading process.
The scenario above is an implication of how Resolving event is implemented by the app. Resolving event implementer needs to own the policy of how it should figure out the loading of an assembly that is already marked missing.
And given that it is trying to perform a load on something that was already determined to be missing (which is why Resolving event was invoked) sounds incorrect.
Agreed.
I believe the confusion stems from the name Resolving, where the developer is likely to assume that hooking into the event will have their logic called for every assembly load, overriding the default assembly discovery logic, while in-fact the event is only called after the default load logic failed (Does it get called when discovery succeeds but loading fails, or only when discovery fails?). If the event was called ResolvingFailed, it would be more intuitive to the developer, who is not intimately familiar with the workings of the framework, about the purpose of the event.
/cc @bleroy would be great to see some samples written for these APIs.
@richlander @bleroy , should we open an issue on the docs repo for a sample on this to track it there?
Closing ... See: https://github.com/dotnet/docs/issues/1711