【问题标题】:MEF Error message - VS2010MEF 错误信息 - VS2010
【发布时间】:2015-11-10 11:14:24
【问题描述】:

谁能帮我解释一下这个错误信息:

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);
        }
    }
}

在为我节省时间提供任何帮助之前,我先感谢您。

【问题讨论】:

    标签: asp.net visual-studio-2010 mef


    【解决方案1】:

    错误消息表示 MEF 正在查找使用接口 IFolderService 导出的类,但容器中没有。

    要对此进行调查,首先检查是否存在导出该接口的类,如果存在,然后查看该类是否被容器拾取,第三,如果这些都不能解决问题,请查看使用接口IFolderService 导出的类是否还有其他一些无法满足的导入。

    【讨论】:

    • @بابکخلیفهسلطان 查看容器创建时的代码,您应该能够调试容器以查看其中包含哪些类型。这是一个新问题吗? IE。这个容器以前工作过吗?我经常发现最简单/最快的方法是查看已进行的更改并尝试查找已更改的导入/导出并尝试以这种方式发现问题。
    • 感谢回复:第一:确实存在导出类;第二:我如何确定是否正在上课?对于第三种情况,出口类又多了三个进口,同样的问题。那么后面这有什么意义呢?
    • 这可能是重点。容器以前工作过。我正在优化应用程序,因此对其进行了升级,将一个新项目(实体)添加到解决方案中,并使用 Itok.DataAccess 更改为使用 Itok.Entities。我想我应该对此更加小心。有什么建议可以解决这个问题?
    • @بابکخلیفهسلطان 我会看看容器是如何设置的,它可能不包括您的新项目。如果您无法深入了解它,那么提出一个新问题可能会更容易,尽管使用一些设置容器的代码。
    【解决方案2】:

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

    感谢 TomDoesCode 查看问题,我希望这能帮助其他遇到类似问题的人。

    此问题的长期解决方案是,如果您有许多满足导入的导出,您可能有两种选择:

    I) 用 [ImportMany] 更改 [Import]。然后在运行时,决定为合约使用哪个导入。问问自己是只选择第一个可用的,还是一次随机使用一个。

    II) 将 [ImportMany] 与元数据结合使用,以决定使用哪个 Import。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-09
      • 2011-02-24
      • 1970-01-01
      • 2017-05-12
      • 2018-07-18
      • 2017-02-19
      • 2011-06-14
      • 2013-05-30
      相关资源
      最近更新 更多