Dotnet Core 简明教程
Managed Extensibility Framework
在本章中,我们将讨论可托管扩展框架 (MEF)。MEF 可用于第三方插件扩展,或者它可以为常规应用程序带来松散耦合的插件式架构的优势。
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.
在本章中,我们将仅了解如何在 .NET Core 应用程序中使用 MEF。
In this chapter, we will only learn how we can use MEF in .NET Core application.
让我们了解一个简单的示例,其中我们将在 .NET Core 控制台应用程序中使用 MEF。现在,让我们创建一个新的 .NET Core 控制台项目。
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.
在左窗格中,选择 Templates → Visual C# → .NET Core,然后在中间窗格中,选择控制台应用程序 (.NET Core)。
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.
创建项目后,我们需要添加对 Microsoft.Composition 的引用,以便我们能够使用 MEF。为此,让我们在解决方案资源管理器中右键单击该项目并 Manage NuGet Packages…
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…
搜索 Microsoft.Composition ,然后单击 Install 。
Search for Microsoft.Composition and click Install.
单击 OK 按钮。
Click the OK button.
单击 I Accept 按钮。
Click the I Accept button.
当安装完成后,您将在“引用”中发现一个错误。
When the installation completes, you will find an error in References.
让我们打开 project.json 文件。
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"
}
}
}
您会看到已添加了 Microsoft.Composition 依赖项,但问题是此程序包与 dnxcore50 不兼容。因此,我们需要导入 portablenet45+win8+wp8+wpa81 。现在,让我们用以下代码替换 project.json 文件。
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.
如果您展开“引用”,就会看到 Microsoft.Composition 的一个引用。
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.
在“名称”字段中输入您类的名称,然后单击 Add 。
Enter the name for your class in the Name field and click Add.
让我们在 PrintData.cs 文件中添加以下代码。
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);
}
}
}
如上所述,在 Microsoft.Composition 命名空间中不提供目录。因此,它会加载装配中具有导出属性的所有类型,并将它们附加到导入属性中,如 Program.cs 文件中的 Compose 方法中所示。
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>();
}
}
}
}
现在,让我们运行您的应用程序,您将看到它正在通过实例化 PrintData 类来运行。
Let us now run your application and you will see that it is running by instantiating the PrintData class.
要详细了解 MEF,让我们访问以下网址 https://msdn.microsoft.com/en-us/library/dd460648%28v=vs.110%29.aspx ,了解更多详情。
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.