Hand-On Labs MEF
06 December 2010 - MEF
This week I will be one of the speakers at BASTA On Tour in Munich. One of the topics I am going to speak about is the Managed Extensibility Framework (MEF). In this blog post I want to share my slides and summarize the hands-on labs that I am going to go through with the participants.
- Download Slides (PDF)
Hands-On Lab 1: Directory Catalog Sample
Prerequisites:
- Visual Studio 2010
Lab step by step description:
- Create a new command line project called DirectoryCatalogDemo.
- The application will ask the user for a string, apply a set of string operations on that string and output the result. The string operations should be extensible. A user should be able to copy an assembly into the program's directory and the application should automatically pick up the assembly and apply all operations that it contains. Therefore the first thing we need is a contract that all string operation parts have to implement.
- Add a new class library project called OperatorContract to your solution.
- Add the following interface to the newly created project:
- Now add an implemenation of IStringOperator to the command line project DirectoryCatalogDemo.
- Add a reference to System.ComponentModel.Composition to the command line project DirectoryCatalogDemo.
- Add the following class to the project:
- Next you have to implement the Program class as follows (note especially how MEF is used to load operators into the member Program.operators using the ComposeParts method):
- Build and run your program. Currently the program will only find the UppercaseOperator part because no other implementations of IStringOperator can be found. However, the application would already load additional operators from assemblies that are present in the application's directory. Next we will create a new operator and add it by just copying the assembly.
- Create a new class library project ReverseStringOperator.
- Add a reference to System.ComponentModel.Composition to the class library.
- Add the following class to the project:
- Build your solution.
- Run the command line application. Try it - the behavior must not have changed because the new operator is unknown by now.
- Copy the assembly with the ReverseStringOperator into the program's directory without stopping the application. Try it - the string must be turned to uppercase and be reversed now.
- Use the debugger to see how MEF loads the assembly during runtime.
This example shows you some basic principles of MEF. However, it could have been implemented much simpler because MEF is not only able to export and import objects. You can use the export/import logic for functions, too. Our operators are just functional (technically func<string, string>). Let us see how we could have made life easier by exporting and importing functions instead of objects:
- Add the following class to your command line project DirectoryCatalogDemo:
- Note the Export attributes on the methods instead of the classes.
- Change the implementation of the Program class as follows (new/changed lines are written in italic):
- Compile and test the application. As you can see MEF correctly exports and imports the methods just as expected.
Hands-On Lab 2: Part Lifecycle Sample
Prerequisites:
- Visual Studio 2010
Lab step by step description:
- Create a new command line project called LifeCycleDemo.
- Add an exported class to the project as follows:
- Change the implementation of the Program class as follows:
- Build your project.
- Run your project in the debugger and try to reconstruct the following important points:
- Lazy parts are constructed at first access.
- ReleaseExports disposes all parts (if they implement IDisposable)
- When is OnImportsSatisfied called?
- What happens if you change the code so that you explicitly dispose the container object?
Hands-On Lab 3: Using MEF To Extend A WPF Application
Prerequisites:
- Visual Studio 2010
- Download and install the latest version of the Visual Studio 2010 and .NET Framework 4 Training Kit
- All the requisites for this lab are verified using the Configuration Wizard. To make sure that everything is correctly configured, follow these steps.
- Note: To perform the setup steps you need to run the scripts in a command window with administrator privileges.
- Run the Configuration Wizard for the Lab if you have not done it previously. To do this, run the CheckDependencies.cmd script located under the Source\Setupfolder of this lab. Install any pre-requisites that are missing (rescanning if necessary) and complete the wizard.
- Note: At the end the wizard will try to install code snippets to complete the lab. You need not install the snippets if you do not want to. Just cancel the wizard at this step to prevent installing software to Visual Studio 2010.
Lab step by step description:
- Open Labs.htm in your favorite browser (IE recommended). You find this file in the installation folder into which you have installed the training kit (see prerequisites above).
- Choose Extensibility in the menu on the left side.
- Click on Launch Lab in the middle column.
- Complete extercises 1 and 2 of the lab.
Hands-On Lab 4: MEF And Silverlight
Prerequisites:
- Same as Hands-On Lab 3
- Additionally: Silverlight 4 Tools for Visual Studio 2010
Lab step by step description:
- Open Labs.htm in your favorite browser (IE recommended). You find this file in the installation folder into which you have installed the training kit (see prerequisites above).
- Choose Extensibility in the menu on the left side.
- Click on Launch Lab in the middle column.
- Complete extercises 3 of the lab.