2015-11-10 19 views
1

任何人都可以帮我解释此错误信息,请:MEF错误信息 - VS2010

system.componentmodel.composition.changerejectedexception 

The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. 
The root cause is provided below. Review the CompositionException.Errors property for more detailed information. 

1) No exports were found that match the constraint: 
ContractName Itok.BusinessLogic.Interfaces.IFolderService 
RequiredTypeIdentity Itok.BusinessLogic.Interfaces.IFolderService 

Resulting in: Cannot set import 'Itok.Web.Photos.Presenters.DefaultPresenter._folderService (ContractName="Itok.BusinessLogic.Interfaces.IFolderService")' on part 'Itok.Web.Photos.Presenters.DefaultPresenter'. 

Element: Itok.Web.Photos.Presenters.DefaultPresenter._folderService (ContractName="Itok.BusinessLogic.Interfaces.IFolderService") --> Itok.Web.Photos.Presenters.DefaultPresenter 

这里是IFolderService.cs:

using System; 
using System.Collections.Generic; 
using Itok.Entities; 

namespace Itok.BusinessLogic.Interfaces 
{ 
    public interface IFolderService 
    { 
     List<Folder> GetFriendsFolders(Int32 AccountID); 
     void DeleteFolder(Folder folder); 
     List<Folder> GetFoldersByAccountID(Int32 AccountID); 
     Folder GetFolderByID(Int64 FolderID); 
     Int64 SaveFolder(Folder folder); 
    } 
} 

这是出口类的定义,FolderService。 cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Itok.BusinessLogic.Interfaces; 
using System.ComponentModel.Composition; 
using Itok.DataAccess.Interfaces; 
using Itok.Common; 
using Itok.DataAccess; 
using Itok.Interfaces; 
using Itok.Entities; 

namespace Itok.BusinessLogic 
{  
    [Export(typeof(IFolderService))]  
    [Export(typeof(ICache))] 
    public class FolderService : IFolderService 
    { 
     [Import] 
     private IFriendRepository _friendRepository; 
     [Import] 
     private IFolderRepository _folderRepository; 
     [Import] 
     private ICache _cacheService; 

     public FolderService() 
     { 
      MEFManager.Compose(this); 
     } 

     public List<Folder> GetFriendsFolders(Int32 AccountID) 
     { 
      List<Friend> friends = _friendRepository.GetFriendsByAccountID(AccountID); 
      List<Folder> folders = _folderRepository.GetFriendsFolders(friends); 
      folders.OrderBy(f => f.CreateDate).Reverse(); 
      return folders; 
     } 

     public void DeleteFolder(Folder folder) 
     { 
      if (_cacheService.Exists(folder.AccountID.ToString())) 
      { 
       _cacheService.Delete(folder.AccountID.ToString()); 
      } 

      _folderRepository.DeleteFolder(folder); 
     } 

     public List<Folder> GetFoldersByAccountID(int AccountID) 
     {   
      List<Folder> cachedFolders = _cacheService.Get(AccountID.ToString()) as List<Folder>; 
      if (cachedFolders != null) 
      { 
       return cachedFolders; 
      } 
      else 
      { 
       cachedFolders = _folderRepository.GetFoldersByAccountID(AccountID); 
       _cacheService.Set(AccountID.ToString(), cachedFolders); 
       return cachedFolders; 
      } 
     } 

     public Folder GetFolderByID(Int64 FolderID) 
     { 
      return _folderRepository.GetFolderByID(FolderID); 
     } 

     public Int64 SaveFolder(Folder folder) 
     { 
      return _folderRepository.SaveFolder(folder); 
     } 
    } 
} 

在节省时间之前,我感谢您的帮助。

回答

0

错误消息表示MEF正在寻找一个与接口IFolderService一起导出的类,但容器中没有该类。

要调查这一点,首先检查是否有一个导出该接口的类,如果存在,则查看该容器是否被该容器拾取,以及第三,如果这两者都不能解决问题,请查看通过接口IFolderService导出的类是否有其他一些不能满足的导入。

+0

@بابکخلیفهسلطان查看容器创建时的代码,您应该能够调试容器以查看容器中的类型。这是一个新问题吗?即这个容器以前是否工作过?我经常发现,查看已经发生了哪些变化,并试图找出哪些进口/出口已经发生变化,并尝试以这种方式发现问题,是最简单/最快的。 – TomDoesCode

+0

感谢您的回应:第一:出口类确实存在;第二:如何确定班级是否被接走?对于第三种情况,出口类还有三个以上的进口产品,同样的问题。那么以后的观点是什么呢? –

+0

这可能就是关键所在。集装箱正在工作。我正在优化应用程序,因此升级了它向解决方案添加了一个新项目(实体),并使用Itok.DataAccess更改为使用Itok.Entities。我想我应该对此更加小心。有任何建议纠正这个问题? –

0

最后,我找到了问题的解决方案。它与MEF指向的IFolderService直接无关。该应用程序依赖于业务逻辑中的组件(FolderService),该组件依次取决于接口ICache和实现包装器Cache.cs。由合同名称Itok.Interfaces.ICache指定的ICache已导出四次(仅在一次导入时)。当我试图扩展解决方案时,这被留意未被注意。 MEF无法确定要使用哪个导出。真正的问题是,MEF正在指向链条上的两个级别!

感谢TomDoesCode寻找问题,我希望这可以帮助其他人得到类似的问题。如果你有很多出口,这将满足一个导入

此问题的一个长期解决办法是,你可能有两种选择:

I)更改[导入]与[ImportMany。然后在运行期间,决定为合同使用哪个导入。问问你自己是不是拿起第一个可用的,或者随机使用一个。

二)使用[ImportMany]结合元数据来决定使用哪个导入。