- .NET Core - Migrations
- Restoring and Building & MSBuild
- .NET Core - MSBuild & project.json
- .NET Core - SDK
- Managed Extensibility Framework
- .NET Core - Testing Library
- Running Tests in Visual Studio
- .NET Core - Create a Testing Project
- .NET Core - PCL Troubleshooting
- Creating a Xamarin.Forms Project
- Sharing .NET Core Libraries
- Adding References to Library
- .NET Core - Portable Class Library
- Create .NET Standard Library
- Windows Runtime & Extension SDKs
- .NET Core - Metapackage
- .NET Core - MSBuild
- Create UWP App with .NET Core
- .NET Core - Package References
- .NET Core - Project Files
- .NET Core - Modularity
- .NET Core - Code Execution
- .NET Core - Garbage Collection
- .NET Core - Numerics
- .NET Core - Getting Started
- .NET Core - Environment Setup
- .NET Core - Prerequisites
- .NET Core - Overview
- .NET Core - Home
.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Managed Extensibipty Framework
In this chapter, we will discuss the Managed Extensibipty Framework (MEF). MEF can be used for third-party plugin extensibipty, or it can bring the benefits of a loosely-coupled plugin-pke architecture to regular apppcations.
MEF is a pbrary for creating pghtweight, extensible apppcations.
It allows apppcation 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 flexibipty, maintainabipty and testabipty of large apppcations.
You can use MEF in your cpent apppcations, whether they use Windows Forms, WPF, or any other technology, or in server apppcations 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 assembpes in a directory.
In this chapter, we will only learn how we can use MEF in .NET Core apppcation.
Let us understand a simple example in which we will use MEF in .NET Core console apppcation. 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 Apppcation (.NET Core).
Enter the name of the project in the Name field and cpck 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-cpck on the project in Solution Explorer and Manage NuGet Packages…
Search for Microsoft.Composition and cpck Install.
Cpck the OK button.
Cpck 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 cpck 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 { pubpc interface IPrintData { void Send(string message); } [Export(typeof(IPrintData))] pubpc class PrintData : IPrintData { pubpc 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 { pubpc class Program { pubpc static void Main(string[] args) { Program p = new Program(); p.Run(); } pubpc void Run() { Compose(); PrintData.Send("Hello,this is MEF demo"); } [Import] pubpc IPrintData PrintData { get; set; } private void Compose() { var assembpes = 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 apppcation and you will see that it is running by instantiating the PrintData class.
To learn more about MEF, let us visit the following Url
for more details. Advertisements