MEF不处理装载的组件

时间:2014-12-30 16:46:40

标签: c# loading mef .net-assembly dispose

我使用MEF从本地目录加载程序集并且它正常工作。后来,当我想要替换其中一个已加载的程序集时,我收到一个异常,说该文件已被另一个进程使用。这是我的代码:

        var moduleList = _service.GetModules();
        List<Module> modules = null;
        string modulesDirPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"PortailModules");

        if (!Directory.Exists(modulesDirPath))
            Directory.CreateDirectory(modulesDirPath);

        using (GenericModuleLoader<IModule> loader = new GenericModuleLoader<IModule>(modulesDirPath))
        {
            modules = new List<Module>(
                      from p in loader.Plugins
                      select new Module
                      {
                          FullPath = p.FullPath,
                          AssemblyName = p.AssemblyName,
                          Version = p.Version
                      });                
        }

在我的GenericModuleLoader中我处理了CompositionContainer。

更换组件:

foreach (var module in moduleList)
            {
                var file = new FileInfo(module.FullPath);
                if (!modules.Any(x => x.AssemblyName == module.AssemblyName))
                {
                    File.Copy(module.FullPath, Path.Combine(modulesDirPath, file.Name), true);
                }
                else
                {
                    if(CompareVersion(module.Version, modules.First(x => x.Name == module.Name).Version) == 1)
                    {
                        File.Copy(module.FullPath, Path.Combine(modulesDirPath, file.Name), true);
                    }
                }
            }

当copyin文件收到例外时。

有什么想法吗?

0 个答案:

没有答案