0

I am putting together a plugin framework with these requirements:

  • load/unload plugins at will
  • call methods in loaded plugins
  • raise callback events from plugin to the owner

To do this I am creating a new AppDomain, and loading the Plugin assemblies into this.

The implementation I have so far is working to a degree, but I believe I am creating an instance of the plugin in the local appDomain and also the new AppDomain.

When I load first, I get duplicate callback messages. When I load/unload multiple times I get multiple callback messages being added to the list. This indicates to me that I am loading up the plugin assembly not only remotely but also locally, and thus my "unload" mechanism is not operating as I would like. I would be grateful if anyone can tell me where I am going wrong.

I also understand I need to take the "life time" of the plugin into account but not sure where to implement this.

Thanks.

(1) I have a plugin interface

public interface IPlugin
{
    string Name();
    string Version();
    string RunProcess();

    // custom event handler to be implemented, event arguments defined in child class
    event EventHandler<PluginEventArgs> CallbackEvent;
    //event EventHandler<EventArgs> CallbackEvent;
    void OnProcessStart(PluginEventArgs data);
    void OnProcessEnd(PluginEventArgs data);
}

(2) custom event args

[Serializable]
public class PluginEventArgs : EventArgs
{
    public string ResultMessage;
    public string executingDomain;

    public PluginEventArgs(string resultMessage = "")
    {
        // default empty values allows us to send back default event response
        this.ResultMessage = resultMessage;
        this.executingDomain = AppDomain.CurrentDomain.FriendlyName;
    }
}

(3) example plugin class implementation

 [Serializable]
    public class Plugin_1 : IPlugin
    {
        System.Timers.Timer counter;
        int TimerInterval;
        string PluginName = "My plugin";

        public string Name()
        {
            return "CMD";
        }

        public bool Start()
        {
            OnStart(new PluginEventArgs());
            RunProcess();
            return true;
        }

        // OnTimer event, process start raised, sleep to simulate doing some work, then process end raised
        public void OnCounterElapsed(Object sender, EventArgs e)
        {
            OnProcessStart(new PluginEventArgs());
            OnProcessEnd(new PluginEventArgs());
            Stop();
        }

        public bool Stop()
        {
            // simulate waiting for process to finish whatever its doing....
            if (counter != null)
            {
                counter.Stop();
                OnStop(new PluginEventArgs());
            }
            return true;
        }

        public string RunProcess()
        {
            TimerInterval = 2000;
            if (counter == null){ 
                counter = new System.Timers.Timer(TimerInterval); 
                }
            else { 
                counter.Stop();
                counter.Interval = TimerInterval;
                }

            counter.Elapsed += OnCounterElapsed;
            counter.Start();
            return "";
        }

        public event EventHandler<PluginEventArgs> CallbackEvent;

        void OnCallback(PluginEventArgs e)
        {
            if (CallbackEvent != null)
            {
                CallbackEvent(this, e);
            }                    
        }


        public void OnProcessStart(PluginEventArgs Data)
        {
            OnCallback(new PluginEventArgs(Data.executingDomain + " - " + PluginName + " started"));   
        }

        public void OnProcessEnd(PluginEventArgs Data)
        {
            OnCallback(new PluginEventArgs(Data.executingDomain + " - " + PluginName + " ended"));   
        }

(4) I have a plugin manager that loads/unloads

public bool LoadPlugin() { try { Domain_Command = AppDomain.CreateDomain("Second_domain"); command_loader = (ProxyLoader)Domain_Command.CreateInstanceAndUnwrap("PluginMgr", "PluginMgr.Method"); Plugins.AddPlugin(command_loader.LoadAndExecute("APluginName", Plugins.ProxyLoader_RaiseCallbackEvent), SomePluginType, false);
return true;
} catch (Exception ex) { string message = ex.Message; return false; } }

(5) my "ProxyLoader" to load the plugin into separate AppDomain

public class ProxyLoader : MarshalByRefObject
{
    public AssemblyInstanceInfo LoadAndExecute(string assemblyName, EventHandler<PluginContract.PluginEventArgs> proxyLoader_RaiseCallbackEvent)
    {
        AssemblyInstanceInfo AInfo = new AssemblyInstanceInfo();
        //nb: this AppDomain.CurrentDomain is in its own context / different from the caller app domain?
        Assembly pluginAssembly = AppDomain.CurrentDomain.Load(assemblyName);
        foreach (Type type in pluginAssembly.GetTypes())
        {
            if (type.GetInterface("IPlugin") != null)
            {
                object instance = Activator.CreateInstance(type, null, null);
                AInfo.ObjectInstance = instance;
                string s = ((PluginContract.IPlugin)instance).RunProcess(); // main procedure
                AInfo.ASM = pluginAssembly;
                ((PluginContract.IPlugin)instance).CallbackEvent += proxyLoader_RaiseCallbackEvent;
                ((PluginContract.IPlugin)instance).Start();
                instance = null;
            }
        }
        return AInfo;
    }
}

(6) I have a callback this plugs into

public event EventHandler<PluginContract.PluginEventArgs> Callback;

void OnCallback(PluginContract.PluginEventArgs e)
        {
            if (Callback != null)
            {
                Callback(this, e);
            }
        }

(7) called by (referenced in ProxyLoader when load the assembly)

public void ProxyLoader_RaiseCallbackEvent(object source, PluginContract.PluginEventArgs e)
{
    OnCallback(new PluginContract.PluginEventArgs(str));
}
qtime67
  • 317
  • 5
  • 21
  • I have a very similar situation. Did you find a solution that you can post here? – Miguel Jun 26 '14 at 21:58
  • 1
    here is is worked out! http://www.codeproject.com/Articles/700435/Windows-service-auto-update-plugin-framework – qtime67 Jun 27 '14 at 13:57

0 Answers0