locked
Usage of .NET collection (Dictionary) in WCF Reflection based data services? RRS feed

  • Question

  • Hello,

    I am trying to simulate one of my application scenarios that is depicted in the following code as follows.

    [DataServiceKey("OrderId")]
    public class Order
    {
       public int OrderId { get; set; }
       public String OrderType { get; set; }
       public Dictionary<String, Item> ItemDictionary { get; set; }
    
    }
    
    [DataServiceKey("ItemId")]
    public class Item
    {
       public int ItemId { get; set; }
    
       public String ItemName { get; set; }
    }
    while doing so, I am getting the following error: "The property 'ItemDictionary' on type 'ODataExample.Order' is not a valid property. Make sure that the type of the property is a public type and a supported primitive type or a entity type with a valid key or a complex type"

    Does this mean that I cannot have a Dictionary collection as one of my reference types in and Reflection based entity? 

    If my understanding is not true, please advise on how to model this situation.

    Thanks,

    Jegan Kunniya

    Friday, July 6, 2012 1:06 PM

Answers

All replies

  • Hi,

    Dictionary is not a supported type in WCF Data Services.

    You can model it using collection properties which were introduced in OData V3 and implemented in WCF Data Services 5.0 (http://blogs.msdn.com/b/astoriateam/archive/2012/04/09/wcf-data-services-5-0-rtm-release.aspx, updates are since available on NuGet http://blogs.msdn.com/b/astoriateam/archive/2012/05/18/wcf-data-services-5-0-1-released.aspx).

    Using a collection property you could define a complex type like:

    public class NamedItem
    {
        public string Name { get; set; }
        public Item Item { get; set; }
    }

    And then use a collection of that complex type instead of the Dictionary:

    public class Order
    {
        ....
        public IEnumerable<NamedItem> ItemDictionary { get; set; }
    }

    Thanks,


    Vitek Karas [MSFT]

    • Proposed as answer by Ashwini47 Friday, July 6, 2012 1:51 PM
    • Marked as answer by Jegan Kunniya Friday, July 6, 2012 6:51 PM
    Friday, July 6, 2012 1:44 PM
    Moderator
  • Thanks Vitek.

    I am currently using the .NET framework 4.0 which i believe supports only ODATA client protocol version V2 and hence the respective couldn't be modeled. Hence I would go with the option that you have suggested. 

    Thanks

    Friday, July 6, 2012 6:41 PM
  • WCF DS 5.0 is fully supported on .NET 4.0, so everything should work. It's just not in the .NET 4.0 itself, you have to install it over. Or use the NuGet packages, that should make it very easy to deploy with the app.

    Thanks,


    Vitek Karas [MSFT]

    Friday, July 6, 2012 7:33 PM
    Moderator
  • Vitek,

    I tried to use the way that you had suggested as NamedItem type, but that too resulted in the following error message "The property 'ItemDictionary' on type 'ODataExample.Order' is not a valid property. Properties whose types are collection of primitives or complex types are not supported".

    Here is what I had done to get the above mentioned error..

    [DataServiceKey("OrderId")]
    public class Order
    {
       public int OrderId { get; set; }
       public String OrderType { get; set; }
       public IEnumerable<NamedItem> ItemDictionary { get; set; }
    
    }
    
    public class NamedItem : IEnumerable<NamedItem>
    {
       public String ItemPurchaseTime { get; set; }
       public Item Item { get; set; }
    
       IEnumerator<NamedItem> IEnumerable<NamedItem>.GetEnumerator()
       {
         ....
       }
    
       System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       {
         ....
       }
    }

    Please suggest if there is a way around with this error? Thanks

    Friday, July 6, 2012 7:34 PM