Description
This proposal adds a Native library resolving event, to be raised when the runtime cannot resolve a native library load.
Proposed API
namespace System.Runtime.Loader
{
public abstract class AssemblyLoadContext
{
/// Event handler for resolving native libraries
/// Inputs: Invoking assembly, and library name to resolve
/// Returns: A handle to the loaded native library
public event Func<Assembly, string, IntPtr> ResolvingUnmanagedDll;
}
}
Rationale
In the case of Loading assemblies, the runtime provides the application to customize the load at various phases:
- Before running the default load logic via
AssemblyLoadContext.Load()
method overrides - After running the default load logic via the
AssemblyLoadContext.Resolving
event handlers.
This proposal creates a matching facility for unmanaged libraries
- Before default DllImport logic: The existing
AssemblyLoadContext.LoadUnmanagedDll()
and separately proposedDllImportResolver
callbacks - After default DllImport logic: The new
AssemblyLoadContext.ResolvingUnmanagedDll
event.
In particular, the NativeLibraryResolve
event is expected to provide flexibility for running custom native library resolution logic from plugins. Hence the motivation to have the event per AssemblyLoadContext, rather than globally.
DllImport sequence
DllImport load library works in the following order, stop at any step the library is successfully loaded.
- If the invoking-assembly has a
DllImportResolver
callback registered, invoke it. - If the invoking-assembly is not in the default load context, call
AssemblyLoadContext.LoadUnmanagedDll()
- Run the default load logic, try loading from:
- AppDomain cache
- NATIVE_DLL_SEARCH_DIRECTORIES
- Invoking-assembly directory, System32, etc. based on
DllImportSearchPaths
- Raise the ResolvingUnmanagedDll event
Discussion
Alternate notation
In this proposal ResolvingUnmanagedDll
uses Func
notation in order to be consistent with the
Loading/Unloading events in AssemblyLoadContext
.
An alternative is to use EventHandler
style recommended by these guidelines, and similar to certain other events in the same class.
namespace System
{
public delegate IntPtr UnmanagedDllResolveEventHandler(object sender, ResolveEventArgs args);
}
namespace System.Runtime.Loader
{
public abstract class AssemblyLoadContext
{
public event UnmanagedDllResolveEventHandler ResolvingUnmanagedDll;
}
}
Related Topics
dotnet/corefx#32015 Native Library Loader API