In this chapter, we will discuss the Managed Extensibility Framework (MEF). MEF can be used for third-party plugin extensibility, or it can bring the benefits of a loosely-coupled plugin-like architecture to regular applications.
MEF is a library for creating lightweight, extensible applications.
It allows application developers to discover and use extensions with no configuration required.
MEF is an integral part of the .NET Framework 4, and is available wherever the .NET Framework is used that improves the flexibility, maintainability and testability of large applications.
You can use MEF in your client applications, whether they use Windows Forms, WPF, or any other technology, or in server applications that use ASP.NET.
MEF has been ported as Microsoft.Composition to .NET Core as well but partially.
Only System.Composition is ported, and System.ComponentModel.Composition is not available yet. This means, we don’t have the catalogs which can load types from assemblies in a directory.
In this chapter, we will only learn how we can use MEF in .NET Core application.
Let us understand a simple example in which we will use MEF in .NET Core console application. Let us now create a new .NET Core console project.
In the left pane, select Templates → Visual C# → .NET Core and then in the middle pane, select Console Application (.NET Core).
Enter the name of the project in the Name field and click OK.
Once the project is created, we need to add reference of Microsoft.Composition so that we can use MEF. To do so, let us right-click on the project in Solution Explorer and Manage NuGet Packages…
Search for Microsoft.Composition and click Install.
Click the OK button.
Click the I Accept button.
When the installation completes, you will find an error in References.
Let us open the project.json file.
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.Composition": "1.0.30", "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } }, "frameworks": { "netcoreapp1.0": { "imports": "dnxcore50" } } }
You can see that the Microsoft.Composition dependency is added, but the problem is that this package is not compatible with dnxcore50. So we need to import portablenet45+win8+wp8+wpa81. Let us now replace your project.json file with the following code.
{ "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { "Microsoft.Composition": "1.0.30", "Microsoft.NETCore.App": { "type": "platform", "version": "1.0.1" } }, "frameworks": { "netcoreapp1.0": { "imports": "portable-net45+win8+wp8+wpa81" } } }
Save this file and you will see that the error is rectified.
If you expand the References, then you will see a reference of Microsoft.Composition.
First we need to create an interface that is to be exported and implement the interface and decorate the class with the export attribute. Let us now add a new class.
Enter the name for your class in the Name field and click Add.
Let us add the following code in the PrintData.cs file.
using System; using System.Collections.Generic; using System.Composition; using System.Linq; using System.Threading.Tasks; namespace MEFDemo { public interface IPrintData { void Send(string message); } [Export(typeof(IPrintData))] public class PrintData : IPrintData { public void Send(string message) { Console.WriteLine(message); } } }
As mentioned above, Catalogs are not available in Microsoft.Composition namespace. So, it will load all the types from the Assembly with export attribute and attach to the import attribute as shown in the Compose method in the Program.cs file.
using System; using System.Collections.Generic; using System.Composition; using System.Composition.Hosting; using System.Linq; using System.Reflection; using System.Threading.Tasks; namespace MEFDemo { public class Program { public static void Main(string[] args) { Program p = new Program(); p.Run(); } public void Run() { Compose(); PrintData.Send("Hello,this is MEF demo"); } [Import] public IPrintData PrintData { get; set; } private void Compose() { var assemblies = new[] { typeof(Program).GetTypeInfo().Assembly }; var configuration = new ContainerConfiguration() .WithAssembly(typeof(Program).GetTypeInfo().Assembly); using (var container = configuration.CreateContainer()) { PrintData = container.GetExport<IPrintData>(); } } } }
Let us now run your application and you will see that it is running by instantiating the PrintData class.
To learn more about MEF, let us visit the following Url https://msdn.microsoft.com/en-us/library/dd460648%28v=vs.110%29.aspx for more details.