AppDomain Assembly not found when loaded from byte array

前端 未结 3 479
清歌不尽
清歌不尽 2021-01-05 11:41

Please bear with me, I spent 30+ hours trying to get this work - but without success.

At the start of my program I load an Assembly (dll) in bytearray and delete it a

3条回答
  •  一整个雨季
    2021-01-05 12:18

    The main problem here is thinking that you can instantiate a plugin while executing code in your primary appdomain.

    What you need to do instead, is create a proxy type which is defined in an already loaded assembly, but instantiated in the new appdomain. You can not pass types across app domain boundaries without the type's assembly being loaded in both appdomains. For instance, if you want to enumerate the types and print to console as you do above, you should do so from code which is executing in the new app domain, not from code that is executing in the current app domain.

    So, lets create our plugin proxy, this will exist in your primary assembly and will be responsible for executing all plugin related code:

    // 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;
        }
    }
    

    A few key points in the comments above I will reiterate here:

    • You must inherit from MarshalByRefObject, this means that the calls to this object can be proxied across app-domain boundaries using remoting.
    • When passing data to or from the proxy class, the data must be marked [Serializable] and also must be in a type which is in the currently loaded assembly. If you need your plugin to return some specific object to you, say PluginResultModel then you should define this class in a shared assembly which is loaded by both assemblies/appdomains.
    • Must pass an assembly qualified type name to CreateAndExecutePluginResult in its current state, but it would be possible to remove this requirement by iterating the assemblies and types yourself and removing the call to Type.GetType.

    Next, you need to create the domain and run the proxy:

    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");
    }
    

    Going to say this again because it's super important and I didn't understand this for a long time: when you're executing a method on this proxy class, such as proxy.LoadAssembly this is actually being serialized into a string and being passed to the new app domain to be executed. This is not a normal function call and you need to be very careful what you pass to/from these methods.

提交回复
热议问题