需要DisallowApplicationBaseProbing时候来装AssemblyResolve事件=真 [英] Need to hookup AssemblyResolve event when DisallowApplicationBaseProbing = true

查看:189
本文介绍了需要DisallowApplicationBaseProbing时候来装AssemblyResolve事件=真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我创建的AppDomain联播的AssemblyResolve事件时,我给自己定DisallowApplicationBaseProbing =真。我这样做的原因是为了强制运行时调用,而不是先试探它需要解析组件AssemblyResolve事件。这样一来,其他开发人员不能仅仅拘泥于MyDllName.dll在ApplicationBase目录,并覆盖我想在AssemblyResolve事件加载程序集。

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

在code永远不会越过+ = OnAssemblyResolve行。

The code never gets past the += OnAssemblyResolve line.

当code尝试执行,新的应用程序域试图解决我在执行大会。因为DisallowApplicationBaseProbing =真实的,它不知道在哪里可以找到该组件。我有一个鸡和放大器;蛋的问题似乎。它需要解决我的组装来装装配解析器,但需要装配解析器来解决我的组装。

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

推荐答案

通过大量的实验,我得到了以下工作:

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

域被这样创建:

    AppDomainSetup setupInfo = AppDomain.CurrentDomain.SetupInformation;
    setupInfo.DisallowApplicationBaseProbing = true;

    domain = AppDomain.CreateDomain("Domain name. ", null, setupInfo);
    AssemblyResolver.Register(domain);

对不起,我不能共享code解决和加载组件。首先,它还不工作。其次,这将是太多,太具体,在此背景下,广大市民共享。

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.

我将自举是序列化,从单个文件需要反序列化的组件旁边的对象结构。对于这一点,我真的应该直接去地狱.dll文件

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.

这篇关于需要DisallowApplicationBaseProbing时候来装AssemblyResolve事件=真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆