Contains runtime components of the Managed Extensibility Framework (MEF).
$ dotnet add package System.Composition.RuntimeSystem.Composition.Runtime is part of the Managed Extensibility Framework (MEF) 2.0, a composition library for .NET that enables dependency injection through attributes or conventions.
This package enables the discovery and composition of parts in applications using MEF 2.0. It offers the runtime implementation needed for managing composable parts, resolving dependencies, and dynamically wiring components together.
Resolve dependencies on the fly and can be useful for dynamically loaded components or plugins.
using System.Composition;
using System.Composition.Hosting;
var configuration = new ContainerConfiguration()
.WithPart<Service>();
using CompositionHost container = configuration.CreateContainer();
var consumer = new Consumer(container);
consumer.Execute();
// Service is running.
public interface IService
{
void Run();
}
[Export(typeof(IService))]
public class Service : IService
{
public void Run() => Console.WriteLine("Service is running.");
}
public class Consumer(CompositionContext context)
{
public void Execute()
{
// Use the context to resolve the service
var service = context.GetExport<IService>();
service.Run();
}
}
The main type provided by this library is:
System.Composition.CompositionContextSystem.Composition.Runtime is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.