locked
Workaround for returning Collection of Complex Types from WCF Data Service RRS feed

  • Question

  • As there is a shortcoming with the current WCF Data Service .Net client library with returning complex data types i worked upon the following workaround

    I have a Entity Model MyEntities and in it I have imported the stored procedure as Function GetProduct and mapped it to a complex type Product

    I have created a class MyCustomEntities and another class CustomProduct which is similar to the complex type Product. I have now created a data service on top of MyCustomEntities. Following is my code

        public class CustomDataService : DataService<MyCustomEntities>
        {
            // This method is called only once to initialize service-wide policies.
            public static void InitializeService(DataServiceConfiguration config)
            {
                config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
                config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
                config.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead);
                config.UseVerboseErrors = true;
            }
    
            [WebGet]
            public List<CustomProduct> GetProductsByCompany(string coId)
            {
                return CurrentDataSource.GetProducts(Convert.ToInt16(coId)).ToList();
            }
            
        }
    
        public class MyCustomEntities
        {
            public IQueryable<CustomProduct> listProduct
            {
                get
                {
                    return new List<CustomProduct>().AsQueryable();
                }
            }
    
    
            public IQueryable<CustomProduct> GetProducts(int coId)
            {
                MyEntities context = new MyEntities();
                var customList = from results in context.GetProduct(coId)
                                    where results.PartNumber != null
                                    select new CustomProduct { PartNumber = results.PartNumber, Description = results.Description, Price = results.Price };
    
                return customList.AsQueryable<CustomProduct>();
            }
        }
    
    
        [DataServiceKeyAttribute("Id")]
        public class CustomProduct
        {
            public int Id { get; set; }
            public string PartNumber { get; set; }
            public string Description { get; set; }
            public decimal Price { get; set; }
        }
    }

    With this approach I am able to return the result of my stored procedure as a List of my custom class. This is not a straight forward way but it is a workaround with wcf data service with reflection provider approach. Can someone please validate this approach and let me know if there are any loopholes in this approach. Your suggestions are important as I want to use this approach in one of my live projects

    Thanks,

    Vandana

    Wednesday, December 5, 2012 10:35 AM

All replies

  • Hi Vandana,

    Please check following link.

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/4a4f173b-6d86-492b-be81-1bd87d166c2f

    According to this "Calling service operations which return collection of complex values is still not supported.".

    We also had the same challenge. What you can do is:

    1) Embed this collection of complex type inside one entity as property (Convert it to JSON and include it as string)

    2) Have some custom annotation which describes what kind of type it is (like ComplexTypeName="CustomProduct" in metadata

    3) Publish this "CustomProduct" as complex type in metadata and then use JSON.NET library to de-serialize this at client side.

    Let me know if this helps.

    Regards,

    Viral Thakkar



    Viral

    • Proposed as answer by ViralT Friday, December 7, 2012 1:46 PM
    Wednesday, December 5, 2012 1:33 PM
  • Or you can use ODataLib (Microsoft.Data.OData on NuGet) to consume either ATOM or JSON response. If you use ODataCollectionReader it will work for service operations returning collections of complex values.

    Thanks,


    Vitek Karas [MSFT]

    Wednesday, December 5, 2012 6:10 PM
    Moderator
  • Or you can use ODataLib (Microsoft.Data.OData on NuGet) to consume either ATOM or JSON response. If you use ODataCollectionReader it will work for service operations returning collections of complex values.

    Thanks,


    Vitek Karas [MSFT]

    It would be extremely helpful if you could post an example of how you would go about doing this and using the ODataCollectionReader class.

    Thanks

    Thursday, December 20, 2012 2:40 PM
  • Or you can use ODataLib (Microsoft.Data.OData on NuGet) to consume either ATOM or JSON response. If you use ODataCollectionReader it will work for service operations returning collections of complex values.

    Thanks,


    Vitek Karas [MSFT]


    @Vitek Karas - I would second the request for a code example on this one.
    Wednesday, July 3, 2013 8:39 PM