|
MefIntegration
Autofac can host MEF extensions via this integration.
Note this integration is no longer distributed with the core download and needs to be built from source. It may be moved to Contrib in V2. IntroductionThe MEF integration allows you to expose extensibility points in your Autofac applications using the Managed Extensibility Framework. Referenced AssembliesTo use MEF in an Autofac application, you must reference both System.ComponentModel.Composition.dll and Autofac.Integration.Mef.dll. Consuming MEF Extensions in AutofacThe Autofac/MEF integration allows MEF catalogs to be registered with the ContainerBuilder. First, the Autofac.Integration.Mef namespace must be included: using Autofac.Integration.Mef; Catalogs containing extensions are registered with the RegisterCatalog() method: var builder = new ContainerBuilder();
var catalog = new DirectoryCatalog("C\MyExtensions");
builder.RegisterCatalog(catalog);All MEF catalog types are supported:
Once MEF catalogs are registered, exports within them can be resolved through the Autofac container or by injection into other components: [Export(typeof(IFoo))]
public class Foo : IFoo { }
// ...
var fooCatalog = new TypeCatalog(typeof(Foo));
builder.RegisterCatalog(fooCatalog);
var container = builder.Build();
var foo = container.Resolve<IFoo>();Providing Autofac Components to MEF ExtensionsAutofac components aren't automatically available for MEF extensions to import. To provide an Autofac component to MEF, the ExportedAs() method must be used: builder.Register<Foo>().ExportedAs<IFoo>(); Implicit Collection SupportIf multiple MEF extensions will support the same extension point, the ImplicitCollectionSupport module will make it easier to consume them. |
Sign in to add a comment