Make your ASP.NET application extendable

by Mads Kristensen 26. July 2007 23:48

People have asked me how we build the extension model into BlogEngine.NET. There’s nothing to it - really, there isn’t.

You need one small class and 14 lines of code in the global.asax. That is all you need to make your ASP.NET application extendable. An extension is just a normal class that is somehow marked as being an extension. No magic is needed.

The scenario I liked best was one where you could just drop your custom extension into the App_Code folder and then it would just work. Reflection would take care of creating an instance of the extension, but we need a way to separate the extensions from all other classes in the App_Code folder. That’s where we need the small class.

The class

I decided it would make most sense to have all custom extensions be decorated with a custom attribute called Extension and then let global.asax activate them when the application starts. That would make the class definition of a custom extension look like this:

[Extension("description")]
public class MyCustomExtension

To do that, we need to write a very small class that inherits from System.Attribute and it looks like this:

public class ExtensionAttribute : System.Attribute
{
  /// <summary>
  /// Creates an instance of the attribute and assigns a description.
  /// </summary>
  public ExtensionAttribute(string description)
  {
    _Description = description;
  }

  private string _Description;
  /// <summary>
  /// Gets the description of the extension.
  /// </summary>
  public string Description
  {
    get { return _Description; }
  }
}

It’s a very simple class that just has a description property and that’s it. The description property is not really needed to implement extensions, so you can leave it out if you’d like.

Global.asax

Now we have our custom attribute, so it’s time to let global.asax use reflection to activate the extensions. This method iterates through all the types in the App_Code folder and when it finds a class that is decorated with the Extension attribute then it creates an instance of it.

void Application_Start(object sender, EventArgs e)
{
  Assembly a = Assembly.Load("__code");
  Type[] types = a.GetTypes();

  foreach (Type type in types)
  {     
    object[] attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);
    foreach (object attribute in attributes)
    {
      a.CreateInstance(type.FullName);
    }
  }
}

That’s it. Now your ASP.NET application handles custom extensions made by who ever wants to write them. There are many ways to customize this implementation. For instance, you can put the reflection code in the Begin_Request method instead of the Application_Start to let your extensions act as an HttpModule.

Download a custom extension (zip)

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags:

ASP.NET

Comments

7/27/2007 10:03:12 PM #

Aaron Fischer

Could you please change your Google ad placement currently its covering text of the first line or so.

Aaron Fischer United States |

7/28/2007 11:21:00 PM #

pingback

Pingback from mhinze.com

7 Links Today (2007-07-28)

mhinze.com |

7/30/2007 6:23:28 AM #

trackback

Trackback from Tiernans Comms Closet

Making your ASP.NET application extendable

Tiernans Comms Closet |

8/2/2007 9:24:39 PM #

trackback

Trackback from C#kes

BlogEngine extension adding smilies to posts

C#kes |

8/9/2007 2:54:10 AM #

trackback

Trackback from MK2

使你的Asp.net具有可扩展性

MK2 |

9/26/2007 6:06:27 AM #

trackback

Trackback from DotNetKicks.com

Make your ASP.NET application extendable

DotNetKicks.com |

9/28/2007 5:53:09 PM #

pingback

Pingback from ajaxninja.com

10 Hot ASP.NET Tips and Tricks - 10/28/2007

ajaxninja.com |

10/2/2007 7:19:28 AM #

pingback

Pingback from netzlab.net

dotnetblogengine.net 1.2 - gut genug, um endlich auch mal mit dem Bloggen anzufangen Wink

netzlab.net |

10/14/2007 3:37:25 AM #

pingback

Pingback from andyskipper.com

andyskipper.com - freelance web developer in london

andyskipper.com |

10/25/2007 6:55:34 PM #

trackback

Trackback from Chris Love's Official Blog - Professional ASP.NET

Links of the Week Oct 24 2007

Chris Love's Official Blog - Professional ASP.NET |

1/24/2008 7:26:45 PM #

pingback

Pingback from mindgravy.net

Mind Gravy  » Blog Archive   » links for 2008-01-24

mindgravy.net |

6/29/2008 5:58:22 PM #

terni

Quick and clever! There is magic in it after all, how do you call it when you do a very useful thing with a bunch of lines of code? Magic it is!
Your blog app is awesome, I would start selling licenses btw.

terni Italy |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008