MEF parts list sometimes empty

随声附和 提交于 2019-12-11 02:45:11

问题


I'm currently using MEF and a DirectoryCatalog to load some parts from some extension DLLs. It works for me, and most of the people that use the program, but some users experience the parts not being loaded at all. Collecting some debug information, it seems that MEF does load the DLLs (catalog.LoadedFiles lists them), but that no parts are listed in catalog.Parts.

One user is on XP sp3 and one is on Windows 7, so I don't think that the OS is the problem. Does anyone have some idea of why this would be happening?

The following is the code that actually creates the container, in case it would help with anything.

        private static IEnumerable<Task> CreateTypes()
    {
        CompositionContainer container = GetContainer();
        var exp = container.GetExports<Task>();
        return exp.Select(e => e.Value);
    }

    private static CompositionContainer container;
    public static CompositionContainer GetContainer()
    {
        if (container != null)
            return container;

        DirectoryCatalog catalog = new DirectoryCatalog(ExtensionDirectory, "*.dll");
        container = new CompositionContainer(catalog);
        return container;
    }

回答1:


(Yes, I'm answering my own question...more than a year later...)

http://mikehadlow.blogspot.com/2011/07/mef-directorycatalog-fails-to-load.html

Basically, because some people downloaded the program using IE then unzipped with Windows Explorer, the DLLs were marked as being from the internet, so MEF refused to load their parts, though they still showed up inside the catalog.

The solution (for my situation at least) was simply to delete the alternate data streams that said that the DLLs were from the internet, as described in the above link.




回答2:


but the other one is in "C:\Spiele" which sounds like a user-created folder

Reminds me of this:

Assembly Load Issues

In .NET, there are different contexts into which an assembly can be loaded. The default load context is generally the best one to use, but it cannot load assemblies that are not in the application base directory, subdirectories of the application base which are included in the probing path, or the GAC. When using a DirectoryCatalog or passing a path to the AssemblyCatalog constructor, MEF will attempt to load assemblies in the default load context. However, if the assemblies are not in the probing path or the GAC, this will not be possible, and MEF will load them in the load-from context instead.

The load-from context can lead to type identity issues that result in an InvalidCastException, MissingMethodException, or other errors. In order to avoid these issues in a MEF application, you can put any extension directories under the application base directory, and add them to the probing private path in your application configuration file. For other options and more information about assembly loading in .NET, see the Best Practices for Assembly Loading MSDN document.

See the source "How to Debug and Diagnose MEF Failures" for more debugging information and tools.



来源:https://stackoverflow.com/questions/9119224/mef-parts-list-sometimes-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!