Custom Plugin Framework in .Net

Sometimes we came in situation when we need to design an architecture which supports plugin of components at runtime. That facilitates developer to not to compile the main application again and again for new addition of components/plugins and just with a little configuration it should be activated in the application. Therefore, this post shows the plugin framework which helps to plug in components with little configuration.

I took the logger example to develop this framework. In which I will show you how to plug in multiple types of logger into the application. The application is very simple just to show an idea which logs the text from the textbox control on the button click event. System checks the plugins that are available and call the ExecuteTask method of each plugin attached with the application.

Let’s start some coding work.

Create Plugin Framework

  1. Create a new class library project in .net to develop a plugin framework.
  2. Create an interface named IPlugin.cs
  3. Define a property “Name” and method “ExecuteTask” which takes String message as a parameter.

    namespace PluginFramework

    {


    public
    interface
    IPlugin

    {


    String name { get; }


    void ExecuteTask(String message);

    }

    }

  4. Actually the concept behind this framework is that developer does not need to reference new plugins dll or class libary they will just add it in the application configuration file. We will provide an specific section for Plugins. Therefore, we need to create a class which implements IConfigurationSectionHandler and define CreateTask method.
  5. The code snippet for PluginSectionHandler as follows

namespace PluginFramework

{


public
class
PluginSectionHandler : IConfigurationSectionHandler

{


ArrayList plugins = new
ArrayList();


public
object Create(object parent, object configContext, System.Xml.XmlNode section)

{


foreach(XmlNode plugin in section.ChildNodes)

{


object plugObject= Activator.CreateInstance(Type.GetType(plugin.Attributes[“type”].Value));

 plugins.Add(plugObject);

}


return plugins;

}

}

}

Create Custom Plugins

  1. Create another class library project named as “MessagePlugin”. This library contains all types of logger which will be added as a plugin In the main application.
  2. I will create two classes one which write the message in the event log and the other write it in the trace window.
  3. Therefore, create a new TraceMessage class and implement the IPlugin interface.
  4. Define the body for ExecuteTask method as follows.

    public
    class
    TraceMessage : IPlugin

    {


    public
    string name

    {


    get { return
    “Trace Logger”; }

    }


    public
    void ExecuteTask(string message)

    {

    System.Diagnostics.Trace.WriteLine(message);

    }

    }

  5. For EventLogMessage plugin, create a new class and implement the IPlugin interface.
  6. Define the body for ExecuteTask method as follows.

    public
    class
    EventLogMessage : IPlugin

    {


    public
    string name

    {


    get

    {


    return
    “Event Logger”;

    }

    }


    public
    void ExecuteTask(String message)

    {


if (!System.Diagnostics.EventLog.SourceExists(“PluginFramework”))

{

System.Diagnostics.EventLog.CreateEventSource(“PluginFramework”, “MyLog”);

}

System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();

log.Source = “PluginFramework”;

log.WriteEntry(message);

}

}

Create Main Application

  1. Create a Windows Application project and add two controls TextBox and a button on the form.

  2. The concept is that developer will not reference the MessagePlugin library created above in the windows application and he will just define the Type and assembly in the app.config file. When the application starts it will load all the sections added. To do this, we will add app.config file first.
  3. Open the app.config file and place a configsections tag and under configsections tag define a new section tag.

    <configSections>

    <section
    name=plugins
    type=PluginFramework.PluginSectionHandler, PluginFramework/>

    </configSections>

  4. Now add the plugins in the configuration file as below.

<plugins>

<plugin
type=MessagePlugin.EventLogMessage, MessagePlugin />

<plugin
type=MessagePlugin.TraceMessage, MessagePlugin />

</plugins>

Complete app.config will look like as follows.

<?xml
version=1.0
encoding=utf-8 ?>

<configuration>

<configSections>

<section
name=plugins
type=PluginFramework.PluginSectionHandler, PluginFramework/>

</configSections>

<plugins>

<plugin
type=MessagePlugin.EventLogMessage, MessagePlugin />

<plugin
type=MessagePlugin.TraceMessage, MessagePlugin />

</plugins>

</configuration>

  1. Override the onload event of form and load the plugins as follows.


    protected
    override
    void OnLoad(EventArgs e)

    {


    base.OnLoad(e);

     LoadPlugins();

    }


    private
    void LoadPlugins()

    {

     pluginList = (ArrayList)ConfigurationSettings.GetConfig(“plugins”);

    }

  2. Now in the button click event call the plugin’s execute task method.


    private
    void btnLog_Click(object sender, EventArgs e)

    {


    foreach (IPlugin plugin in pluginList)

    {

    plugin.ExecuteTask(textBox1.Text);

    }

    }

  3. Place the MessagePlugins dll or class library in the bin directory of windows application and run the application to test.

For the complete code please email me at ovaismehboob@yahoo.com.