locked
MEF unsupported cycle during composition #MEF #WinRT #Metro RRS feed

  • Question

  • I am using MEF for web and Windows store apps v. 1.0.15 and I require a cyclic composition on my items.


        [Export]
        public class ItemHolder
        {
            [ImportMany]
            public IEnumerable<IItem> Items { get; set; }
        }
    
        public interface IItem
        {
            ItemHolder Holder { get; set; }
        }
    
        [Export(typeof(IItem))]
        public class ItemA : IItem
        {
            [Import]
            public ItemHolder Holder { get; set; }
        }
    
        [Export(typeof(IItem))]
        public class ItemB : IItem
        {
            [Import]
            public ItemHolder Holder { get; set; }
        }
    
        [Export(typeof(IItem))]
        public class ItemC : IItem
        {
            [Import]
            public ItemHolder Holder { get; set; }
        }

    In the above scenario I can access any IItem from any IItem as required, but when I am getting composition Exception.

    Detected an unsupported cycle for part 'ItemHolder'. To construct a valid cycle, at least one part in the cycle must be shared, and at least one import in the cycle must be non-prerequisite (e.g. a property).
     -> required by import 'Holder' of part 'ItemA'
     -> required by import 'item' of part 'IItem[]'
     -> required by import 'Items' of part 'ItemHolder'
     -> required by initial request for contract 'ItemHolder'

    I am lost here, why part of cycle should be shared? It use to work in MEF (.NET framework 4.0) and how do I resolve it now?


    Imran Shaik


    • Edited by urxlnc Thursday, September 6, 2012 12:03 PM title missing
    Thursday, September 6, 2012 12:02 PM

Answers

  • Parts are not Exported as Shared by default in MEF for Metro. Should attatch attribute [Export, Shared] for exports.

    Answer here MEF Codeplex



    Imran Shaik


    • Edited by urxlnc Friday, September 7, 2012 12:10 PM
    • Marked as answer by urxlnc Friday, September 7, 2012 12:10 PM
    Friday, September 7, 2012 12:10 PM