locked
Loading (Success) and unloading (fail) an AppDomain [For plugin purpose] RRS feed

  • Question

  • In my PluginManager.cs file I can succefully load my plugin in its own appDomain using this code:

     

      AppDomain d= AppDomain.CreateDomain(loadPlugin.Name);
                AssemblyLoader loader = (AssemblyLoader)d.CreateInstanceAndUnwrap("PluginManager", "PluginManager.AssemblyLoader");
    loader.LoadAndExecute(DllFilePath, this as IHost);
    
    

     


    The AssemblyLoader looks like this

     

    public class AssemblyLoader : MarshalByRefObject
        {
           
            internal void LoadAndExecute(string DllFileToStart, IHost host)
            {
               string assemblyName = "";
                try
                {
                    Assembly w = Assembly.LoadFrom(DllFileToStart);
                    assemblyName = w.GetName().Name;
                    Type[] pluginTypes = w.GetTypes();
                    foreach (Type t in pluginTypes)
                    {
                        if (t.GetInterface("IPlugin")!=null)
                        {
                            IPlugin temp = (IPlugin)Activator.CreateInstance(t);
                         temp.Host = host;
                           temp.StartPlugin();
    
                        }
                    }
    
                }
                catch
                {
    
                }
            }
        }
    

     


    Now in the IPlugin interface there is a method called PluginClosed

    which is called on the Host (ie pluginManager)

    So when the plugin closes it will execute this code in the pluginManager.

    public void PluginStopped(IPlugin plugin)

            {

               AppDomain.Unload(d);

            }

    In this code i would like to unload the appdomain d for the plugin.

    But when i try this i get:

    "The application domain in which the thread was running has been unloaded"

    So how can i trigger an unload event when the plugin closes in a good way?


    • Edited by Tobias_K Thursday, December 15, 2011 7:18 AM missing text
    • Moved by Leo Liu - MSFT Thursday, December 22, 2011 2:58 AM Off-Topic (From:Visual C# General)
    Thursday, December 15, 2011 6:57 AM

All replies

  • Hi,

    if I understood you correctly, you try to unload an assembly from inside the assembly? That cannot work. You are executing code in the assembly when you try to unload it!

    See http://msdn.microsoft.com/en-us/library/c5b8a8f9.aspx for more information. The important part is: "When you have finished using an application domain". So you must make sure that it is no longer in use!

    With kind regards,

    Konrad

    Thursday, December 15, 2011 7:38 AM
  • Hmmm ok this is how i want it to work...

     

    I want the plugin (Which is loaded in its own AppDomain) to be able to notify the PluginManager (Which is running in my main program domain) that it has been closed so the pluginmanager can do AppDomain(Unload the plugin domain).....

    Does this make any sense :) ?

    Well i think you have a point, but how can the pluginmanager be notified so that it can unload the plugin appdomain?

    All examples i've managed to google do this:

    AppDomain domain = AppDomain.CreateDomain("MyDomain", null);
    
            Console.WriteLine("Host domain: " + AppDomain.CurrentDomain.FriendlyName);
            Console.WriteLine("child domain: " + domain.FriendlyName);
            AppDomain.Unload(domain);

    They load the domain and in the same method the unload the domain. Since my plugin is a control i would like to unload it later when the user press the "Close" button on the plugin....

     

    So basically I have problems to notify my pluginmanager when to do AppDomain.Unload(PluginDomain)

     

    • Edited by Tobias_K Thursday, December 15, 2011 7:46 AM edited
    Thursday, December 15, 2011 7:43 AM
  • Hi Tobias,

    How is it going now with your issue?
    Reflection related issue are supported in the dedicated .NET Base Class Library Forum, if it could not get resolved here, please post there for better support.

    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, December 20, 2011 8:06 AM