MEF: “Unable to load one or more of the requested types. Retrieve the LoaderExceptions for more information”

前端 未结 3 637
抹茶落季
抹茶落季 2020-12-25 15:03

Scenario: I am using Managed Extensibility Framework to load plugins (exports) at runtime based on an interface contract defined in a separate dll. In my Visual Studio solut

3条回答
  •  温柔的废话
    2020-12-25 15:38

    Here is an example of above mentioned methods:

    var di = new DirectoryInfo(Server.MapPath("../../bin/"));
    
            if (!di.Exists) throw new Exception("Folder not exists: " + di.FullName);
    
            var dlls = di.GetFileSystemInfos("*.dll");
            AggregateCatalog agc = new AggregateCatalog(); 
    
            foreach (var fi in dlls)
            {
                try
                {
                    var ac = new AssemblyCatalog(Assembly.LoadFile(fi.FullName));
                    var parts = ac.Parts.ToArray(); // throws ReflectionTypeLoadException 
                    agc.Catalogs.Add(ac);
                }
                catch (ReflectionTypeLoadException ex)
                {
                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }
            }
    
            CompositionContainer cc = new CompositionContainer(agc);
    
            _providers = cc.GetExports();
    

提交回复
热议问题