问题
I need to hookup an AssemblyResolve event on my created AppDomain when I've set DisallowApplicationBaseProbing = true. The reason I'm doing this is to force the runtime to call the AssemblyResolve event it needs to resolve an assembly, instead of probing first. This way, another developer can't just stick MyDllName.dll in the ApplicationBase directory and override the assembly I wanted to load in the AssemblyResolve event.
The issue with doing this is the following...
class Program
{
static void Main()
{
AppDomainSetup ads = new AppDomainSetup();
ads.DisallowApplicationBaseProbing = true;
AppDomain appDomain = AppDomain.CreateDomain("SomeDomain", null, ads);
appDomain.AssemblyResolve += OnAssemblyResolve;
appDomain.DoCallBack(target);
}
static System.Reflection.Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
{
Console.WriteLine("Hello");
return null;
}
private static void target()
{
Console.WriteLine(AppDomain.CurrentDomain);
}
}
The code never gets past the += OnAssemblyResolve line.
When the code tries to execute, the new app domain tries to resolve the assembly I'm executing in. Because DisallowApplicationBaseProbing = true, it has no idea where to find this assembly. I have a chicken & egg problem it seems. It needs to resolve my assembly to hookup the assembly resolver but needs the assembly resolver to resolve my assembly.
Thanks for any and all help.
-Mike
回答1:
With lots of experiments I got the following working:
internal class AssemblyResolver : MarshalByRefObject
{
static internal void Register(AppDomain domain)
{
AssemblyResolver resolver =
domain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location,
typeof(AssemblyResolver).FullName) as AssemblyResolver;
resolver.RegisterDomain(domain);
}
private void RegisterDomain(AppDomain domain)
{
domain.AssemblyResolve += ResolveAssembly;
domain.AssemblyLoad += LoadAssembly;
}
private Assembly ResolveAssembly(object sender, ResolveEventArgs args)
{
// implement assembly resolving here
return null;
}
private void LoadAssembly(object sender, AssemblyLoadEventArgs args)
{
// implement assembly loading here
}
}
The domain is created like this:
AppDomainSetup setupInfo = AppDomain.CurrentDomain.SetupInformation;
setupInfo.DisallowApplicationBaseProbing = true;
domain = AppDomain.CreateDomain("Domain name. ", null, setupInfo);
AssemblyResolver.Register(domain);
Sorry, I can't share the code for resolving and loading the assemblies. Firstly, it doesn't work yet. Secondly, it will be too much and too specific to share with the general public in this context.
I'll be bootstrapping an object structure that is serialized alongside the assemblies required for deserialization from a single file. For this, I really deserve going straight to .dll hell.
来源:https://stackoverflow.com/questions/11122052/need-to-hookup-assemblyresolve-event-when-disallowapplicationbaseprobing-true