如何在AssemblyResolve中使用AppDomain.CreateDomain?
我想通过使用内存从WCF加载我的程序集.一切正常,何时:
I want to load my assemblies from WCF by using memory. 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);
但是我想使用AppDomain.CreateDomain,而不是当前域:
But I want to use AppDomain.CreateDomain, not the current domain:
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);
}
我使用以下两个类库,ClassLibrary1和ClassLibrary2:
I have two class libraries, ClassLibrary1 and ClassLibrary2 using the below:
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取决于ClassLibrary1.所以我正在使用assemblyresolver.但是我在 ApplicationHost.Load(binary); :
Classlibrary2 depends on ClassLibrary1. So I am using assemblyresolver. But I get an error on ApplicationHost.Load(binary);:
错误:无法加载文件或程序集'ClassLibrary1,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空'或其依赖项之一.系统找不到指定的文件.
此外,它也不会点火.我的光标不会转到Assemblyresolver方法.如何使用带有resolve方法的AppDomain.CreateDomain?
Also it is NOT FIRING ASSEMBLYRESOLVER. My cursor is not going to the Assemblyresolver method. How do I use AppDomain.CreateDomain with the resolve method?
我个人不喜欢从字节数组中加载程序集.我认为最好将程序集保存到一个临时文件夹,然后从该文件夹中加载它们.看一下本文: 应用程序域很难…… .
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….