[英]How do I use AppDomain.CreateDomain with AssemblyResolve?
I want to load my assemblies from WCF by using memory. 我想通过使用内存从WCF加载我的程序集。 Everything is working good WHEN:
一切都很好,当时:
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly[] assBefore = AppDomain.CurrentDomain.GetAssemblies();
foreach (byte[] binary in deCompressBinaries)
loadedAssembly = AppDomain.CurrentDomain.Load(binary);
But I want to use AppDomain.CreateDomain, not the current domain: 但我想使用AppDomain.CreateDomain,而不是当前域:
protected void LoadApplication()
{
this.ApplicationHost = AppDomain.CreateDomain("TestService", null, new AppDomainSetup
{
ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase
});
ApplicationHost.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve);
foreach (AssemblyName asmbly in System.Reflection.Assembly.GetExecutingAssembly().GetReferencedAssemblies())
{
ApplicationHost.Load(asmbly);
}
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);
}
I have two class libraries, ClassLibrary1 and ClassLibrary2 using the below: 我有两个类库,ClassLibrary1和ClassLibrary2使用如下:
namespace ClassLibrary2
{
public class Class1 : MarshalByRefObject
{
public Class1()
{
}
public int GetSum(int a , int b)
{
try
{
ClassLibrary1.Class1 ctx = new ClassLibrary1.Class1();
return ctx.Sum(a, b);
}
catch
{
return -1;
}
}
public int GetMultiply(int a, int b)
{
return a * b;
}
}
}
Classlibrary2 depends on ClassLibrary1. Classlibrary2依赖于ClassLibrary1。 So I am using assemblyresolver.
所以我使用的是assemblyresolver。 But I get an error on ApplicationHost.Load(binary);
但我在ApplicationHost.Load(二进制)上收到错误; :
:
Error: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
错误: 无法加载文件或程序集“ClassLibrary1,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”或其依赖项之一。 The system cannot find the file specified.
该系统找不到指定的文件。
Also it is NOT FIRING ASSEMBLYRESOLVER. 它也不是FIRING ASSEMBLYRESOLVER。 My cursor is not going to the Assemblyresolver method.
我的光标不会转到Assemblyresolver方法。 How do I use AppDomain.CreateDomain with the resolve method?
如何将AppDomain.CreateDomain与resolve方法一起使用?
I personally don't like loading assemblies from a byte array. 我个人不喜欢从字节数组加载程序集。 I think it is better to save your assemblies to a temporary folder and then load them from that folder.
我认为最好将程序集保存到临时文件夹,然后从该文件夹加载它们。 Take a look at this article: Application Domains is hard… .
看一下这篇文章: Application Domains很难......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.