MEF导出是否在每次请求时都缓存或发现

本文关键字:缓存 发现 请求 是否 MEF | 更新日期: 2023-09-27 17:52:40

如果我有一个MyClass类型,注册

[Export(typeof(Myclass))]属性,和

[PartCreationPolicy(CreationPolicy.Shared)]

[PartCreationPolicy(CreationPolicy.NonShared)]

和稍后尝试调用

compositionContainer.GetExportedValue<Myclass>()多次。

问题:第一次调用,我将通过MEF获得我注册的类-查找所有注册的程序集,然后尝试找到一个注册的合同。问题是关于第二次等等- MEF会再次进行全局查找还是在内部缓存?

MEF导出是否在每次请求时都缓存或发现

MEF会再次进行全局查找或者在内部缓存

是的,MEF执行一些缓存并广泛使用延迟初始化,如果您对MEF性能有疑问:

1)元数据(可组合部件、导出定义和导入定义)被缓存。例子:

public override IEnumerable<ExportDefinition> ExportDefinitions
{
    get
    {
        if (this._exports == null)
        {
            ExportDefinition[] exports = this._creationInfo.GetExports().ToArray<ExportDefinition>();
            lock (this._lock)
            {
                if (this._exports == null)
                {
                    this._exports = exports;
                }
            }
        }
        return this._exports;
    }
}

2)导出的也被缓存:

public object Value
{
    get
    {
        if (this._exportedValue == Export._EmptyValue)
        {
            object exportedValueCore = this.GetExportedValueCore();
            Interlocked.CompareExchange(ref this._exportedValue, exportedValueCore, Export._EmptyValue);
        }
        return this._exportedValue;
    }
}

当然,当使用CreationPolicy.NonShared时,当您请求它时,将一次又一次地创建导出值。但即使在这种情况下,也不会执行"全局查找",因为元数据无论如何都是缓存的。

每次使用[PartCreationPolicy(CreationPolicy.NonShared)]时都进行查找。然后你必须自己实现缓存。

默认实现是使用Singleton模式。这等于属性[PartCreationPolicy(CreationPolicy.Shared)]这是最佳实践

更多信息,请阅读http://blogs.microsoft.co.il/blogs/bnaya/archive/2010/01/09/mef-for-beginner-part-creation-policy-part-6.aspx

虽然值/元数据可能被部分缓存,但做一些性能测试表明,每次调用GetExportedValue时都会执行一些查找。因此,如果你有很多需要获取值的调用,你应该自己做缓存。

namespace MEFCachingTest
{
    using System;
    using System.ComponentModel.Composition;
    using System.ComponentModel.Composition.Hosting;
    using System.ComponentModel.Composition.Primitives;
    using System.Diagnostics;
    using System.Reflection;
    public static class Program
    {
        public static CompositionContainer Container { get; set; }
        public static ComposablePartCatalog Catalog { get; set; }
        public static ExportedClass NonCachedClass
        {
            get
            {
                return Container.GetExportedValue<ExportedClass>();
            }
        }
        private static ExportedClass cachedClass;
        public static ExportedClass CachedClass
        {
            get
            {
                return cachedClass ?? (cachedClass = Container.GetExportedValue<ExportedClass>());
            }
        }
        public static void Main()
        {
            Catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            Container = new CompositionContainer(Catalog);
            const int Runs = 1000000;
            var stopwatch = new Stopwatch();
            // Non-Cached.
            stopwatch.Start();
            for (int i = 0; i < Runs; i++)
            {
                var ncc = NonCachedClass;
            }
            stopwatch.Stop();
            Console.WriteLine("Non-Cached: Time: {0}", stopwatch.Elapsed);
            // Cached.
            stopwatch.Restart();
            for (int i = 0; i < Runs; i++)
            {
                var cc = CachedClass;
            }
            stopwatch.Stop();
            Console.WriteLine("    Cached: Time: {0}", stopwatch.Elapsed);
        }
    }
    [Export]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class ExportedClass
    {
    }
}

更多的变化,看看这个要点:https://gist.github.com/DanielRose/d79f0da2ef61591176ce

在我的电脑上,Windows 7 x64, .NET 4.5.2:

Non-Cached: Time: 00:00:02.1217811
    Cached: Time: 00:00:00.0063479

使用MEF 2 from NuGet:

Non-Cached: Time: 00:00:00.2037812
    Cached: Time: 00:00:00.0023358

在我工作的实际应用程序中,这使应用程序慢了6倍。