gpt4 book ai didi

c# - 从字节数组加载时找不到 AppDomain 程序集

转载 作者:太空狗 更新时间:2023-10-30 00:22:50 25 4
gpt4 key购买 nike

请耐心等待,我花了 30 多个小时来完成这项工作 - 但没有成功。

在我的程序开始时,我在 bytearray 中加载一个程序集 (dll),然后将其删除。

_myBytes = File.ReadAllBytes(@"D:\Projects\AppDomainTest\plugin.dll");

稍后在程序中我创建一个新的 Appdomain,加载字节数组并枚举类型。

var domain = AppDomain.CreateDomain("plugintest", null, null, null, false);

domain.Load(_myBytes);

foreach (var ass in domain.GetAssemblies())
{
Console.WriteLine($"ass.FullName: {ass.FullName}");
Console.WriteLine(string.Join(Environment.NewLine, ass.GetTypes().ToList()));
}

正确列出类型:

ass.FullName: plugin, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null

...

Plugins.Test

...

现在我想在新的 AppDomain 中创建该类型的实例

domain.CreateInstance("plugin", "Plugins.Test");

此调用导致 System.IO.FileNotFoundException,我不知道为什么。

当我查看 .NET Assemblies -> Appdomain: plugintest 下的 ProcessExplorer 时,我发现该程序集已正确加载到新的 appdomain 中。

我怀疑发生异常是因为在磁盘上再次搜索程序集。但是为什么程序要重新加载呢?

如何使用从字节数组加载的程序集在新的应用程序域中创建实例?

最佳答案

这里的主要问题是认为您可以在主应用程序域中执行代码时实例化插件。

您需要做的是创建一个代理类型,该代理类型在已加载的程序集中定义,但在 应用程序域中实例化。如果类型的程序集未加载到两个应用程序域中,您不能跨应用程序域边界传递类型。 例如,如果你想像上面那样枚举类型并打印到控制台,你应该从在新应用程序域中执行的代码中执行,而不是从在当前应用程序域中执行的代码中执行 .

因此,让我们创建我们的插件代理,它将存在于您的主程序集中,并将负责执行所有与插件相关的代码:

// Mark as MarshalByRefObject allows method calls to be proxied across app-domain boundaries
public class PluginRunner : MarshalByRefObject
{
// make sure that we're loading the assembly into the correct app domain.
public void LoadAssembly(byte[] byteArr)
{
Assembly.Load(byteArr);
}

// be careful here, only types from currently loaded assemblies can be passed as parameters / return value.
// also, all parameters / return values from this object must be marked [Serializable]
public string CreateAndExecutePluginResult(string assemblyQualifiedTypeName)
{
var domain = AppDomain.CurrentDomain;

// we use this overload of GetType which allows us to pass in a custom AssemblyResolve function
// this allows us to get a Type reference without searching the disk for an assembly.
var pluginType = Type.GetType(
assemblyQualifiedTypeName,
(name) => domain.GetAssemblies().Where(a => a.FullName == name.FullName).FirstOrDefault(),
null,
true);

dynamic plugin = Activator.CreateInstance(pluginType);

// do whatever you want here with the instantiated plugin
string result = plugin.RunTest();

// remember, you can only return types which are already loaded in the primary app domain and can be serialized.
return result;
}
}

上面评论中的几个要点我在这里重申一下:

  • 您必须继承自 MarshalByRefObject ,这意味着可以使用远程处理跨应用程序域边界代理对该对象的调用。
  • 当向代理类传递数据或从代理类传递数据时,数据必须标记为[Serializable]。并且还必须是当前加载的程序集中的类型。如果你需要你的插件返回一些特定的对象给你,说 PluginResultModel那么您应该在由两个程序集/应用程序域加载的共享程序集中定义此类。
  • 必须将程序集限定的类型名称传递给 CreateAndExecutePluginResult在当前状态下,但可以通过自己迭代程序集和类型并删除对 Type.GetType 的调用来删除此要求.

接下来,您需要创建域并运行代理:

static void Main(string[] args)
{
var bytes = File.ReadAllBytes(@"...filepath...");
var domain = AppDomain.CreateDomain("plugintest", null, null, null, false);
var proxy = (PluginRunner)domain.CreateInstanceAndUnwrap(typeof(PluginRunner).Assembly.FullName, typeof(PluginRunner).FullName);
proxy.LoadAssembly(bytes);
proxy.CreateAndExecutePluginResult("TestPlugin.Class1, TestPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
}

再说一遍,因为它非常重要,我很长一段时间都没有理解这一点:当你在这个代理类上执行一个方法时,比如 proxy.LoadAssembly这实际上被序列化为一个字符串,并被传递到要执行的新应用程序域。这不是正常的函数调用,您需要非常小心传递给这些方法或从这些方法传递的内容。

关于c# - 从字节数组加载时找不到 AppDomain 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127992/

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