Managed Extensibility Framework (MEF)

[TODO: Add documentation about RegisterMetadataRegistrationSources()]

The Autofac MEF integration allows you to expose extensibility points in your applications using the Managed Extensibility Framework.

To use MEF in an Autofac application, you must reference the .NET framework System.ComponentModel.Composition.dll assembly and get the Autofac.Mef package from NuGet.

Consuming MEF Extensions in Autofac

The Autofac/MEF integration allows MEF catalogs to be registered with the ContainerBuilder, then use the RegisterComposablePartCatalog() extension method.

var builder = new ContainerBuilder();
var catalog = new DirectoryCatalog(@"C:\MyExtensions");
builder.RegisterComposablePartCatalog(catalog);

All MEF catalog types are supported:

  • TypeCatalog
  • AssemblyCatalog
  • DirectoryCatalog

Once MEF catalogs are registered, exports within them can be resolved through the Autofac container or by injection into other components. For example, say you have a class with an export type defined using MEF attributes:

[Export(typeof(IService))]
public class Component : IService { }

Using MEF catalogs, you can register that type. Autofac will find the exported interface and provide the service.

var catalog = new TypeCatalog(typeof(Component));
builder.RegisterComposablePartCatalog(catalog);
var container = builder.Build();

// The resolved IService will be implemented
// by type Component.
var obj = container.Resolve<IService>();

Providing Autofac Components to MEF Extensions

Autofac components aren’t automatically available for MEF extensions to import. To provide an Autofac component to MEF, the Exported() extension method must be used:

builder.RegisterType<Component>()
       .Exported(x => x.As<IService>().WithMetadata("SomeData", 42));