我真的好奇为什么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);
}
答案 0 :(得分:3)
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();
需要加入AssemblyResolve
。该块当前将所有内容加载到AppDomain中,因此您必须重新编写该逻辑。
当然,您可能想做的另一件事是不重新发明轮子,并使用ILMerge。
答案 1 :(得分:2)
AssemblyResolve 只有CLR的填充火灾无法加载程序集的原因之一。此事件不会因为从字节数组加载程序集而触发。
以下是一些例子。
此代码将触发 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");