vs-mef

repository·main·Indexed 19 days ago

https://github.com/microsoft/vs-mef

A high-performance implementation of the .NET Managed Extensibility Framework (MEF) used by the Visual Studio team. It includes the core engine (Microsoft.VisualStudio.Composition), Roslyn analyzers for identifying common MEF errors (Microsoft.VisualStudio.Composition.Analyzers), a cached MEF graph system for improved startup performance (Microsoft.VisualStudio.Composition.AppHost), and a diagnostic tool for inspecting catalogs and compositions (Microsoft.VisualStudio.Composition.VSMefx).

Tokens
21.5K
Snippets
49
Records
91
Agent score
67%

What's inside vs-mef

  1. Overview of vs-mef packages

    main

    The vs-mef repository contains the Visual Studio team's implementation of .NET's managed extensibility framework (MEF). It is distributed via several specialized NuGet packages depending on your requirements:

    • Microsoft.VisualStudio.Composition: The core high-performance MEF engine. It supports both System.ComponentModel.Composition and System.Composition attributes.
    • Microsoft.VisualStudio.Composition.Analyzers: Roslyn analyzers designed to help MEF consumers identify common errors in MEF parts during development.
    • Microsoft.VisualStudio.Composition.AppHost: Provides a VS MEF system that utilizes a pre-computed, cached MEF graph for improved performance.
    • Microsoft.VisualStudio.Composition.VSMefx: A diagnostic tool used to inspect catalogs and compositions to troubleshoot MEF-related issues.
  2. Overview of Microsoft.VisualStudio.Composition

    main

    Microsoft.VisualStudio.Composition is a high-performance MEF (Managed Extensibility Framework) host designed to replace or augment existing MEF implementations. It is optimized for speed and provides advanced lifetime management features.

    Key capabilities include:

    • High-performance hosting: A faster host for existing MEF parts.
    • Attribute compatibility: You can reuse your existing MEF attributes.
    • Scoped lifetimes: Supports ExportFactory<T> to create sub-containers with scoped lifetimes, allowing you to define and enforce sharing boundaries within your application.
  3. Use Microsoft.VisualStudio.Composition.Analyzers to prevent MEF authoring errors

    main
    The Microsoft.VisualStudio.Composition.Analyzers package provides a suite of analyzers and suppressors designed to catch common mistakes when authoring MEF (Managed Extensibility Framework) parts. These tools help ensure that exports and imports are correctly defined and compatible with the MEF runtime.
  4. Use Microsoft.VisualStudio.Composition.Analyzers to identify MEF errors

    main
    The Microsoft.VisualStudio.Composition.Analyzers package provides a set of Roslyn analyzers designed to help MEF (Managed Extensibility Framework) consumers identify common configuration and implementation errors in MEF parts. These analyzers catch issues ranging from attribute mismatches to incorrect constructor usage during development.
  5. Understand the core benefits of VS MEF

    main

    VS MEF is a high-performance Managed Extensibility Framework (MEF) implementation designed to overcome the performance and compatibility limitations of standard .NET MEF (System.ComponentModel.Composition) and NuGet MEF (System.Composition).

    Key advantages include:

    • High Throughput: Uses a fully precomputed and validated composition graph for maximum speed when constructing exports.
    • Compositional Diagnostics: Provides a complete list of diagnostics that identify rejected MEF parts, including root causes and cascading effects.
    • Fast Startup via Serialization: Both the catalog and composition can be serialized after creation and deserialized in subsequent application instances. This allows for extremely fast startup times by bypassing assembly loading, scanning, and composition computation.
    • Cross-MEF Compatibility: Acts as a bridge between .NET MEF and NuGet MEF, allowing parts written for either system to share a common composition (e.g., a NuGet MEF part can import exports from a .NET MEF part and vice versa).
    • Standalone Capability: Despite its name, VS MEF is a standalone library that can be hosted in unit tests or any application requiring high-performance extensibility, independent of Visual Studio.
  6. Define sub-scopes using NuGet MEF attributes in VS MEF

    main

    VS MEF adopts the NuGet MEF model for managing sub-scopes (containers).

    Unlike .NET MEF, which requires custom hosting code to manage scopes via unique catalogs, NuGet MEF uses "sharing boundaries" that allow MEF parts to define sub-scopes themselves.

    To define a MEF part that can create new sub-scope instances in VS MEF, you must use NuGet MEF attributes. These attributes are the mechanism used to describe sharing boundaries.

  7. Identify subtle MEF attribute mismatches with custom attributes

    main

    A violation can occur even if you don't explicitly mix namespaces in your code, if you use a custom export attribute that inherits from a different MEF variety than your imports.

    For example, if a custom attribute ExportFireAttribute inherits from the MEF v1 System.ComponentModel.Composition.ExportAttribute, but your class uses [Import] from the MEF v2 System.Composition namespace, the part will be defective. The engine will see the MEF v1 export but will fail to recognize the MEF v2 imports.

    // Custom attribute inheriting from MEF v1
    using System.ComponentModel.Composition;
    
    public class ExportFireAttribute : ExportAttribute
    {
        public ExportFireAttribute() : base(typeof(IFire)) { }
    }
    
    // Defective usage: Custom MEF v1 export + MEF v2 import
    using System.Composition;
    using MyVendor;
    
    [ExportFireAttribute]
    public class FastFire : IFire
    {
        [Import]
        public IWater Water { get; set; }
    }
  8. How interface metadata views work

    main

    An interface metadata view is the baseline model for consuming export metadata through a strongly-typed contract. It is the most predictable model when you want the metadata contract itself to define the filtering behavior during composition.

    Filtering Rules:

    • Properties without [DefaultValue] are considered required. Required properties filter exports by both presence and type compatibility.
    • Properties with [DefaultValue] are considered optional. Optional properties allow the metadata key to be absent; however, if the key is present, its value must still be type-compatible.
    // Baseline model: Interface metadata view
    public interface IInterfaceMetadataView
    {
        // Required: Export must have this key and it must be a string
        string RequiredProperty { get; }
    
        // Optional: Export can omit this, but if present, it must be an int
        [DefaultValue(0)]
        int OptionalProperty { get; }
    }
  9. Understand the ComposableCatalog concept

    main

    A ComposableCatalog is the foundational collection of all MEF parts (types) that the framework will activate and initialize. MEF parts are typically classes decorated with [Export] and [Import] attributes.

    To populate a ComposableCatalog, you use a class derived from PartDiscovery. This class scans assemblies to create ComposablePartDefinition instances. VS MEF supports multiple PartDiscovery implementations, allowing you to mix parts defined with standard .NET MEF attributes and those defined with NuGet MEF attributes within a single catalog.

  10. Understand the Microsoft Support Policy

    main
    Microsoft support for this software is restricted to its use in officially supported products, such as Visual Studio. Support and servicing are limited to the latest released version. For professional assistance, you can open a ticket with the Microsoft assisted support team.