gpt4 book ai didi

c# - 如何将 AppDomain.CreateDomain 与 AssemblyResolve 一起使用?

转载 作者:行者123 更新时间:2023-11-30 18:39:27 25 4
gpt4 key购买 nike

我想使用内存从 WCF 加载我的程序集。一切正常 时间:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
Assembly[] assBefore = AppDomain.CurrentDomain.GetAssemblies();
foreach (byte[] binary in deCompressBinaries)
loadedAssembly = AppDomain.CurrentDomain.Load(binary);

但我想使用 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);
}

我有两个类库,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;
}
}
}

类库 2 依赖于类库 1。所以我正在使用 assemblyresolver。但是我在 ApplicationHost.Load(binary); 上遇到错误:

Error: Could not load file or assembly 'ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

它也不是 FIRING ASSEMBLYRESOLVER。我的光标不会转到 Assemblyresolver 方法。如何将 AppDomain.CreateDomain 与 resolve 方法一起使用?

最佳答案

我个人不喜欢从字节数组加载程序集。我认为最好将程序集保存到一个临时文件夹,然后从该文件夹加载它们。看看这篇文章: Application Domains is hard… .

关于c# - 如何将 AppDomain.CreateDomain 与 AssemblyResolve 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9721686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com