core icon indicating copy to clipboard operation
core copied to clipboard

How to handle assemblies that fail to resolve now there is no DomainContext.AssemblyResolve event?

Open patricknolan opened this issue 10 years ago • 17 comments

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

patricknolan avatar Oct 18 '16 21:10 patricknolan

Would anyone know?

patricknolan avatar Oct 20 '16 12:10 patricknolan

Could someone please provide an answer here? I'm guessing that no answer means that this very important capability has been overlooked in dotnetcore?

patricknolan avatar Oct 28 '16 13:10 patricknolan

Does anyone know?

patricknolan avatar Nov 25 '16 12:11 patricknolan

I am also interested in an answer on this topic.

SeanSnyders avatar Dec 01 '16 20:12 SeanSnyders

does anyone know the answer?

Praburaj avatar Feb 03 '17 20:02 Praburaj

@gkhanna79 Any pointers for AssemblyLoadContext usage?

mellinoe avatar Feb 03 '17 21:02 mellinoe

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

gkhanna79 avatar Feb 03 '17 21:02 gkhanna79

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.

nosider avatar Feb 16 '17 04:02 nosider

Can you please share a repro for this?

Also, can you please move this issue to CoreCLR repo?

CC @rahku

gkhanna79 avatar Feb 16 '17 15:02 gkhanna79

@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.

nosider avatar Feb 16 '17 20:02 nosider

To understand this better, please share a repro for the recursion you are running into.

gkhanna79 avatar Feb 16 '17 20:02 gkhanna79

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.

nosider avatar Feb 16 '17 21:02 nosider

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.

gkhanna79 avatar Feb 16 '17 21:02 gkhanna79

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.

nosider avatar Feb 16 '17 21:02 nosider

/cc @bleroy would be great to see some samples written for these APIs.

richlander avatar Mar 10 '17 08:03 richlander

@richlander @bleroy , should we open an issue on the docs repo for a sample on this to track it there?

Petermarcu avatar Mar 11 '17 04:03 Petermarcu

Closing ... See: https://github.com/dotnet/docs/issues/1711

richlander avatar Mar 11 '17 04:03 richlander