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.

81 thoughts on “Custom Plugin Framework in .Net

  1. Following study a handful of the websites on your personal internet web site now, and that i genuinely like your technique for blogging. I bookmarked it to my bookmark site list and you are going to be checking back soon. Pls look at my internet page too and told me what you believe.

  2. Hi there very cool blog!! Man .. Excellent .. Superb .. I will bookmark your web site and take the feeds also…I am happy to search out so many helpful information right here in the submit, we need work out more strategies on this regard, thanks for sharing.

  3. I like the helpful information you provide for your articles. I’ll bookmark your weblog and take a look at once more right here regularly. I’m somewhat certain I will learn lots of new stuff right right here! Best of luck for the following!

  4. I would like to thank you for the efforts you have put in writing this web site. I’m hoping the same high-grade website post from you in the upcoming as well. Actually your creative writing abilities has encouraged me to get my own site now. Actually the blogging is spreading its wings quickly. Your write up is a great example of it.

  5. I’ve recently started a site, the information you provide on this site has helped me greatly. Thanks for all of your time & work. “It is no use saying, ‘We are doing our best.’ You have got to succeed in doing what is necessary.” by Sir Winston Churchill.

  6. Wohh just what I was looking for, regards for posting . “Be nice to everyone on your way to the top because you pass them all on the way down.” by Fred Hufnagel, Sr..

  7. I like this web site very much, Its a really nice berth to read and receive information. “Everything is vague to a degree you do not realize till you have tried to make it precise.” by Bertrand Russell.

  8. I would like to express my appreciation to the writer just for bailing me out of such a dilemma. After surfing through the the web and finding methods which are not helpful, I thought my entire life was done. Living devoid of the approaches to the difficulties you’ve resolved through your main article is a crucial case, and those which may have negatively affected my career if I had not come across your blog post. The training and kindness in maneuvering all the things was crucial. I’m not sure what I would’ve done if I had not encountered such a solution like this. It’s possible to at this moment look forward to my future. Thanks a lot so much for your specialized and amazing guide. I will not hesitate to endorse the website to any person who needs counselling on this matter.

  9. Its such as you read my mind! You seem to grasp so much about this, such as you wrote the e-book in it or something. I think that you can do with a few % to pressure the message home a little bit, however other than that, this is great blog. A great read. I’ll certainly be back.

  10. I am extremely inspired with your writing skills and also with the layout on your blog. Is that this a paid theme or did you modify it your self? Anyway stay up the nice high quality writing, it’s rare to peer a nice blog like this one nowadays.

  11. What’s Taking place i am new to this, I stumbled upon this I have discovered It absolutely useful and it has aided me out loads. I hope to give a contribution & assist different customers like its aided me. Great job.

  12. I really like your writing style, superb information, appreciate it for posting :D. “In university they don’t tell you that the greater part of the law is learning to tolerate fools.” by Doris Lessing.

  13. Great blog! I really love how it’s easy on my eyes as well as the information are well written. I am wondering how I could be notified whenever a new post has been made. I have subscribed to your rss feed which ought to do the trick! Have a nice day!

  14. I really loved the post so I used my Digg account to digg it.. Such a type of blog post will definitely click to numerous viewers. A good article and useful for its written content. Many thanks for sharing it up! pleasing .

  15. Its such as you learn my thoughts! You appear to understand a lot approximately this, like you wrote the e-book in it or something. I think that you just could do with a few percent to drive the message home a little bit, however instead of that, this is wonderful blog. A fantastic read. I will definitely be back.

  16. I just checked the Nudia page on iTunes under podcasts to manually download and its not showing up in the list. Jorges interview is first on the list, next is the Sunset on the Beach trailer.

  17. Great post, I conceive website owners should larn a lot from this website its rattling user genial . “My father always told me, ‘Find a job you love and you’ll never have to work a day in your life.'” by Jim Fox.

  18. Thank you so much with regard to giving me an update on this matter on your web page. Please realise that if a brand new post appears or if any improvements occur about the current post, I would be thinking about reading more and finding out how to make good use of those strategies you share. Thanks for your efforts and consideration of other people by making your blog available.

  19. I wanted to visit and allow you to know how really I liked discovering your site today. I would consider it a great honor to operate at my business office and be able to make real use of the tips provided on your web site and also get involved in visitors’ responses like this. Should a position of guest author become available at your end, please let me know.

  20. Unquestionably consider that which you stated. Your favorite reason appeared to be at the web the simplest factor to understand of. I say to you, I certainly get irked whilst other people consider worries that they just do not recognize about. You managed to hit the nail upon the top and also defined out the entire thing without having side effect , other folks could take a signal. Will probably be back to get more. Thank you!

  21. Just want to say your article is as amazing. The clearness for your put up is simply excellent and that i can suppose you’re a professional in this subject. Well along with your permission let me to snatch your feed to keep updated with impending post. Thanks a million and please keep up the gratifying work.

  22. Many thanks for being my personal teacher on this theme. I enjoyed your article very much and most of all enjoyed how you handled the aspect I regarded as controversial. You’re always really kind towards readers really like me and assist me to in my everyday living. Thank you.

  23. The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice.

  24. Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  25. Can I just say what a aid to find somebody who truly is aware of what theyre speaking about on the internet. You undoubtedly know the way to deliver a difficulty to mild and make it important. More people have to read this and perceive this side of the story. I cant believe youre no more well-liked because you undoubtedly have the gift.

  26. I found your weblog website on google and check a number of of your early posts. Proceed to keep up the excellent operate. I just extra up your RSS feed to my MSN News Reader. In search of ahead to studying more from you later on!…

  27. Great post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  28. My brother recommended I might like this blog. He was totally right. This post actually made my day. You can not imagine just how much time I had spent for this info! Thanks!

  29. I wanted to write a small message to say thanks to you for all of the marvelous strategies you are placing on this website. My particularly long internet investigation has at the end been recognized with reasonable insight to talk about with my guests. I would point out that most of us readers actually are very much endowed to be in a great website with so many outstanding professionals with interesting basics. I feel very happy to have used your web site and look forward to many more amazing minutes reading here. Thanks a lot once again for everything.

Leave a Reply to seo services Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s