Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c# - Need to hookup AssemblyResolve event when DisallowApplicationBaseProbing = true

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...