dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.06k stars 4.69k forks source link

MEF ComposeParts doesn't work in .NET Core #38500

Open programmersommer opened 4 years ago

programmersommer commented 4 years ago

Hi,

I am trying two following simple examples:

[System.Composition.Import("ICalculator")]
public ICalculator Calculator { get; set; }

 var pluginAssemblies = Directory.GetFiles(Path.Combine(_env.ContentRootPath, "plugins"), "*.dll", SearchOption.TopDirectoryOnly)
                .Select(AssemblyLoadContext.Default.LoadFromAssemblyPath);

 var configuration = new ContainerConfiguration().WithAssemblies(pluginAssemblies);

 using (var container = configuration.CreateContainer())
 {
      CalculateVAT = container.GetExports<ICalculateVAT>().FirstOrDefault();
 }

This one is working. But not the next one:

 var pluginsCatalog = new DirectoryCatalog(Path.Combine(_env.ContentRootPath, "plugins"));

 var container = new CompositionContainer(pluginsCatalog);
 container.ComposeParts(this);

Is ComposeParts (System.ComponentModel.Composition) ported to .NET Core?

ghost commented 4 years ago

Tagging subscribers to this area: @ViktorHofer Notify danmosemsft if you want to be subscribed.

ghost commented 4 years ago

Tagging subscribers to this area: @ViktorHofer Notify danmosemsft if you want to be subscribed.

ericstj commented 4 years ago

Yes, ComposeParts is ported. https://github.com/dotnet/runtime/blob/54a09d24142864767d50c83c2a64c0e1e37a34c2/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/AttributedModelServices.cs#L149

Please provide specific information about the problem you are seeing, along with a repro project. We need more information than "This one is working. But not the next one"

programmersommer commented 4 years ago

@ericstj, please check next one project https://github.com/programmersommer/MEFApp

In AccountantService I would like to use two folders/catalogs with libraries and set priority to one of them. I know that it could be possible with CompositionContainer. But even when I try to use CompositionContainer in a simple way with one catalog - it does not working.