English 中文(简体)
Managed Extensibility Framework
  • 时间:2024-11-05

Managed Extensibipty Framework


Previous Page Next Page  

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.

Name field

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.

Manage

Cpck the OK button.

Cpck the Button

Cpck the I Accept button.

Accept

When the installation completes, you will find an error in References.

Error 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.

Rectified

If you expand the References, then you will see a reference of Microsoft.Composition.

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.

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.

PrintData

To learn more about MEF, let us visit the following Url https://msdn.microsoft.com/en-us/pbrary/dd460648%28v=vs.110%29.aspx for more details.

Advertisements