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

Jen*_*nny 4 .net c# clr appdomain c#-4.0

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

在我的程序开始时,我在bytearray中加载一个Assembly(dll)并在之后删除它.

_myBytes = File.ReadAllBytes(@"D:\Projects\AppDomainTest\plugin.dll");
Run Code Online (Sandbox Code Playgroud)

稍后在程序中我创建一个新的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()));
}
Run Code Online (Sandbox Code Playgroud)

类型得到正确列出:

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

...

Plugins.Test

...

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

domain.CreateInstance("plugin", "Plugins.Test");
Run Code Online (Sandbox Code Playgroud)

这个电话会产生,System.IO.FileNotFoundException我不知道为什么.

当我查看ProcessExplorer时,.NET Assemblies -> Appdomain: plugintest我看到程序集在新的appdomain中正确加载.

我怀疑发生异常,因为在磁盘上再次搜索程序集.但为什么该程序想再次加载它?

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

cae*_*say 7

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

您需要做的是创建一个代理类型,该代理类型在已加载的程序集中定义,但在新的 appdomain中实例化.如果没有在两个appdomains中加载类型的程序集,则无法跨应用程序域边界传递类型. 例如,如果要枚举类型并按上述方式打印到控制台,则应该从在新应用程序域中执行的代码执行此操作,而不是从当前应用程序域中执行的代码执行此操作.

所以,让我们创造我们的插件的代理,这样会存在于你的主要组件,并负责执行所有插件相关的代码:

// 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

以上评论中的几个要点我将在此重申:

  • 您必须继承MarshalByRefObject,这意味着可以使用远程处理跨应用程序域边界代理对此对象的调用.
  • 向代理类传递数据或从代理类传递数据时,必须标记数据,并且数据必须是[Serializable]当前加载的程序集中的类型.如果你需要你的插件向你返回一些特定的对象,PluginResultModel那么你应该在一个由程序集/ appdomains加载的共享程序集中定义这个类.
  • 必须将程序集限定类型名称传递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");
}
Run Code Online (Sandbox Code Playgroud)

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