In .NET Core 2.2, you can use a startup hook to inject code before running an application's Main
method.
System.Runtime.Loader.AssemblyLoadContext
behavior.This could be used with AssemblyLoadContext
APIs to resolve dependencies, not on the TPA list from a shared location, similar to the GAC on a full framework.
AssemblyLoadContext
could make this easier to use by making the default load context or TPA list modifiable.The StartupHook
type is internal and in the global namespace, and the signature of the Initialize
method is public static void Initialize()
.
internal class StartupHook
{
public static void Initialize()
{
AssemblyLoadContext.Default.Resolving += SharedHostPolicy.SharedAssemblyResolver.LoadAssemblyFromSharedLocation;
}
}
namespace SharedHostPolicy
{
class SharedAssemblyResolver
{
public static Assembly LoadAssemblyFromSharedLocation(AssemblyLoadContext context, AssemblyName assemblyName)
{
string sharedAssemblyPath = ""; // find assemblyName in shared location...
if (sharedAssemblyPath != null)
return AssemblyLoadContext.Default.LoadFromAssemblyPath(sharedAssemblyPath);
return null;
}
}
}