为什么AssemblyResolve不起作用

本文关键字:不起作用 AssemblyResolve 为什么 | 更新日期: 2023-09-27 18:28:04

我真的很想知道为什么assemblyResolver不工作?我也不能使用

foreach(deCompressBinaries中的byte[]二进制)
ApplicationHost.Load(二进制);

如何解雇AssemblyResolve?请看我的参考问题:http://stackoverflow.com/questions/9721686/how-to-use-appdomain-createdomain-with-assemblyresolve

    protected void LoadApplication()
    {
        AppDomainSetup domainSetup = new AppDomainSetup();
        domainSetup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
        domainSetup.DisallowBindingRedirects = false;
        domainSetup.DisallowCodeDownload = true;
        domainSetup.LoaderOptimization = LoaderOptimization.SingleDomain;
        //domainSetup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
        ApplicationHost = AppDomain.CreateDomain("Test.Service", null, domainSetup);
        object obj = ApplicationHost.CreateInstanceAndUnwrap("Test.Process", "Test.ApplicationLoader");
        Assembly objExecutingAssemblies = Assembly.GetExecutingAssembly();
        AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();
        foreach (AssemblyName assName in arrReferencedAssmbNames)
        {
            ApplicationHost.Load(assName);
        }

        ApplicationHost.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
        List<byte[]> deCompressBinaries = new List<byte[]>();
        foreach (var item in AppPackage.Item.AssemblyPackage)
            deCompressBinaries.Add(item.Buffer);
        var decompressvalues =  DeCompress(deCompressBinaries);
        deCompressBinaries.Clear();
        deCompressBinaries = decompressvalues.ToList();
        foreach (byte[] binary in deCompressBinaries)
            ApplicationHost.Load(binary);
        Assembly[] assAfter = AppDomain.CurrentDomain.GetAssemblies();
    }
    Assembly OnAssemblyResolve(object sender, ResolveEventArgs args)
    {
        return Assembly.Load(args.Name);
    }

为什么AssemblyResolve不起作用

AssemblyResolve永远不会激发,因为您正在加载LoadApplication方法中的所有程序集-只有在未能解析所需的程序集引用时才会调用AssemblyResolve

我建议运行fuslogvw.exe,这样你就可以直观地看到正在发生的事情。

如果你想根据需要加载程序集,代码块:

List<byte[]> deCompressBinaries = new List<byte[]>();
foreach (var item in AppPackage.Item.AssemblyPackage)
deCompressBinaries.Add(item.Buffer);
var decompressvalues =  DeCompress(deCompressBinaries);
deCompressBinaries.Clear();
deCompressBinaries = decompressvalues.ToList();
foreach (byte[] binary in deCompressBinaries)
    ApplicationHost.Load(binary);
Assembly[] assAfter = AppDomain.CurrentDomain.GetAssemblies();

将需要被工作到CCD_ 4中。该块当前将EVERYTHING加载到AppDomain中,因此您必须重新处理这段逻辑。

当然,您可能想做的另一件事是不要重新发明轮子,而是使用ILMerge。

AssemblyResolveCLR的仅填充激发由于以下原因之一而无法加载程序集。不会因为从字节数组加载程序集而触发此事件。

下面是一些例子。

  • 此代码将触发AssemblyResolve事件,因为"System.Drawing"作为程序集名称是不够的。

    object obj2 = ApplicationHost.CreateInstanceAndUnwrap("System.Drawing", "System.Drawing.Rectangle");
    
  • 此代码不会触发AssemblyResolve事件,因为"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"是完全限定的程序集名称。

    object obj2 = ApplicationHost.CreateInstanceAndUnwrap("System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Rectangle");