Click here to Skip to main content
15,886,110 members
Articles / Web Development / ASP.NET

MEF with ASP.NET - "Hello World!"

Rate me:
Please Sign up or sign in to vote.
4.84/5 (18 votes)
12 Mar 2011CPOL12 min read 65.8K   1.2K   41  
Basic MEF (Managed Extensibility Framework) with ASP.NET. Extreme barebones "Hello World!" example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;



namespace aspnetMEFBasic {
  public partial class aspnetMEFBasic : System.Web.UI.Page {
    //Import our "plugin"
    [Import]
    Class1 c1;

    protected void Page_Load(object sender, EventArgs e) {
      //Step 1:
      //Find the assembly (.dll) that has the stuff we need (i.e. [Export]ed stuff) and put it in our catalog
      DirectoryCatalog catalog = new DirectoryCatalog(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin"));

      //Step 2:
      //To do anything with the stuff in the catalog, we need to put into a container (Which has methods to do the magic stuff)
      CompositionContainer container = new CompositionContainer(catalog);

      //Step 3:
      //Now lets do the magic bit - Wiring everything up
      container.ComposeParts(this);

      //Step4:
      //Lets see if it works
      div1.InnerText = c1.s1;

    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions