Create Managed Extensibility Framework Plug-Ins
The Managed Extensibility Framework (MEF) is a library for creating lightweight, extensible applications.
For up-to-date information regarding MEF, refer to the MSDN article “Managed Extensibility Framework.”
Prerequisites
Before running this example, keep the following in mind:
You must be running at least Microsoft® Visual Studio® 2010 to create MEF applications. If you can't use Microsoft Visual Studio 2010, you can't run this example code, or any other program that uses MEF. End users do not need Microsoft Visual Studio 2010 to run applications using MEF.
You must be running at least Microsoft .NET Framework 4.0 to use the MEF feature.
The easiest way to use MEF is through the type-safe API.
Addition and Multiplication Applications with MEF
This MEF example application consists of an MEF host and two parts. The parts
implement a very simple interface (ICompute
) which defines three
overloads of a single function (compute
).
Each part performs simple arithmetic. In one part, the compute function adds one (1) to its input. In the other part, compute multiplies its input by two (2). The MEF host loads both parts and calls their compute functions twice.
To run this example, you’ll create a new solution containing three projects:
MEF host
Contract interface assembly
Strongly-typed metadata attribute assembly
Create an MEFHost Assembly
Start Microsoft Visual Studio 2010.
Click File > New > Project.
In the Installed Templates pane, click Visual C# to filter the list of available templates.
Select the Console Application template from the list.
In the Name field, enter
MEFHost
.Click OK. Your project is created.
Replace the contents of the default
Program.cs
with theMEFHost.cs
code. For information about locating example code, see “Where to Find Example Code,” above.In the Solution Explorer pane, select the project MEFHost and right-click. Select Add Reference.
Click Assemblies > Framework and add a reference to
System.ComponentModel.Composition
.To prevent security errors, particularly if you have a non-local installation of MATLAB®, add an application configuration file to the project. This XML file instructs the MEF host to trust assemblies loaded from the network. If your project does not include this configuration file, your application fails at run time.
Select the MEFHost project in the Solution Explorer pane and right-click.
Click Add > New Item.
From the list of available items, select Application Configuration File.
Name the configuration file
App.config
and click Add.Replace the automatically-generated contents of
App.config
with this configuration:<?xml version="1.0" encoding="utf-8" ?> <configuration> <runtime> <loadFromRemoteSources enabled="true" /> </runtime> </configuration>
You have finished building the first project, which builds the MEF host.
Next, you add a C# class library project for the MEF contract interface assembly.
Create a Contract Interface Assembly
in Visual Studio, click File > New > Project.
In the Installed Templates pane, click Visual C# to filter the list of available templates.
Select the Class Library template from the list.
In the Name field, enter
Contract
.Note
Ensure
Add to solution
is selected in the Solution drop-down box.Click OK. Your project is created.
Replace the contents of the default
Class1.cs
with the followingICompute
interface code:namespace Contract { public interface ICompute { double compute(double y); double[] compute(double[] y); double[,] compute(double[,] y); } }
You have finished building the second project, which builds the Contract Interface Assembly.
Since strongly-typed metadata requires that you decorate MEF parts with a custom
metadata attribute, in the next step you add a C# class library project. This project
builds an attribute assembly to your MEFHost
solution.
Create a Metadata Attribute Assembly
in Visual Studio, click File > New > Project.
In the Installed Templates pane, click Visual C# to filter the list of available templates.
Select the Class Library template from the list.
In the Name field, enter
Attribute
.Note
Ensure
Add to solution
is selected in the Solution drop-down box.Click OK. Your project is created.
In the generated assembly code, change the namespace from
Attribute
toMEFHost
. Your namespace code should now look like the following:In the MEFHost namespace, replace the contents of the default class
Class1.cs
with the following code for theComputationTypeAttribute
class:using System.ComponentModel.Composition; [MetadataAttribute] [AttributeUsage(AttributeTargets.Class, AllowMultiple=false)] public class ComputationTypeAttribute: ExportAttribute { public ComputationTypeAttribute() : base(typeof(Contract.ICompute)) { } public Operation FunctionType{ get; set; } public double Operand { get; set; } } public enum Operation { Plus, Times }
Navigate to the .NET tab and add a reference to
System.ComponentModel.Composition.dll
.
Add Contract and Attributes References to MEFHost
Before compiling your code in Microsoft Visual Studio:
In your MEFHost project, add references to the Contract and Attribute projects.
In your Attribute project, add a reference to the Contract project.
Compile Your Code in Microsoft Visual Studio
Build all your code by selecting the solution name MEFHost in the Solution Explorer pane, right-clicking, and selecting Build Solution.
In doing so, you create the following binaries in
MEFHost/bin/Debug
:
Attribute.dll
Contract.dll
MEFHost.exe
Write MATLAB Functions for MEF Parts
Create two MATLAB functions. Each must be named compute
and stored in
separate folders, within your Microsoft
Visual Studio project:
Create Metadata Files
Create a metadata file for each MATLAB function.
For
MEFHost/Add/compute.m
:Name the metadata file
MEFHost/Add/Add.metadata
.In this file, enter the following metadata on one line:
[MEFHost.ComputationType(FunctionType=MEFHost.Operation.Plus, Operand=1)]
For
MEFHost/Multiply/compute.m
:Name the metadata file
MEFHost/Multiply/Multiply.metadata
.In this file, enter the following metadata on one line:
[MEFHost.ComputationType(FunctionType=MEFHost.Operation.Times, Operand=2)]
Build .NET Components from MATLAB Functions and Metadata
In this step, use the Library Compiler app to create .NET components from the MATLAB functions and associated metadata.
Use the information in these tables to create both Addition
and
Multiplication
projects.
Note
Since you are deploying two functions, you need to run the Library
Compiler app twice, once using the
Addition.prj
information and once using the
Multiplication.prj
information.
Addition.prj
Library Name | Addition |
Class Name | Add |
File to Compile | MEFHost/Add/compute.m |
Multiplication.prj
Library Name | Multiplication |
Class Name | Multiply |
File to Compile | MEFHost/Multiply/compute.m |
Create your component, following the instructions in Generate .NET Assembly and Build .NET Application.
Modify project settings ( > Settings) on the Type Safe API tab, for whatever project you are building (
Addition
orMultiplication
).Project Setting Addition.prj
Multiplication.prj
Enable Type Safe API Checked Checked Interface Assembly MEFHost/bin/Debug/Contract.dll
MEFHost/bin/Debug/Contract.dll
MEF metadata MEFHost/Add/Add.metadata
MEFHost/Multiply/Multiply.metadata
Attribute Assembly MEFHost/bin/Debug/Attribute.dll
MEFHost/bin/Debug/Attribute.dll
Wrapped Class Add
Multiply
Click the Package button.
Install MEF Parts
The two components you have built are MEF parts. You now need to move the generated parts into the catalog directory so your application can find them:
Create a parts folder named
MEFHost/Parts
.If necessary, modify the path argument that is passed to the
DirectoryCatalog
constructor in your MEF host program. It must match the full path to theParts
folder that you just created.Note
If you change the path after building the MEF host a first time, you must rebuild the MEF host again to pick up the new
Parts
path.Copy the two
s (component
Native.dllAddition
andMultiplication
) andAddICompute.dll
andMultiplyICompute.dll
assemblies from your intoMEFHost/Parts
.Note
You do not need to reference any of your MEF part assemblies in the MEF host program. The host program uses a
DirectoryCatalog
, which means it automatically searches for (and loads) parts that it finds in the specified folder. You can add parts at any time, without having to recompile or relink the MEF host application. You do not need to copyAddition.dll
orMultiplication.dll
to theParts
directory.
Run the MEF Host Program
MATLAB-based MEF parts require MATLAB Runtime, like all deployed MATLAB code.
Before you run your MEF host, ensure that the correct version of MATLAB Runtime is available and that
is on your path.matlabroot
/runtime/arch
From a command window, run the following. This example assumes you are running from
c:\Work
.c:\Work> MEFHost\bin\Debug\MEFHost.exe
Verify you receive the following output:
8 Plus 1 = 9 9 Times 2 = 18 16 Plus 1 = 17 1.5707963267949 Times 2 = 3.14159265358979
Note
If you receive an exception indicating that a type initializer failed, ensure that you have .NET security permissions set to allow applications to load assemblies from a network.