none
Unable to return custom class from WCF Data Service

    Question

  • I am trying to return a custom class from my WCF data service. My custom class is:

    [DataServiceKey("ID")]
    public class Applist {
      public int ID { get; set; }
      public string Name { get; set; }
    }
    

    My data service looks like:

    public static void InitializeService(IDataServiceConfiguration config)
    {
      config.RegisterKnownType(typeof(Applist));
      config.SetEntitySetAccessRule("*", EntitySetRights.All);
      config.SetServiceOperationAccessRule("GetApplications", ServiceOperationRights.AllRead);
    }
    
    [WebGet]
    public IQueryable<Applist> GetApplications() {
      var result = (from p in this.CurrentDataSource.Applications
         orderby p.ApplicationName
         group p by p.ApplicationName into g
         select new Applist { ID = g.Min(p => p.id), Name = g.Key });
    
      return result.AsQueryable();
    }
    

    However when I run the service, it gives me an error:

    Request Error Request Error The server encountered an error processing the request. 
    The exception message is 'Unable to load metadata for return type
    'System.Linq.IQueryable`1[ApplicationService.Applist]' of method
    'System.Linq.IQueryable`1[ApplicationService.Applist] GetApplications()'
    

    The same query runs perfectly fine in LINQPad.

    Monday, October 04, 2010 3:19 AM

All replies

  • Hi,

    Which version of the framework are you using?  I tried it on 4.0 and it seems to work.  Note that Applist will be treated as a complex type as it does not belong to any set.

    Thanks,

    -J

    Tuesday, October 05, 2010 12:35 AM
  • I am using VS 2008 3.5 with SP1. Basically its a ADO.Net Data Service
    Tuesday, October 05, 2010 3:50 AM
  • Can you trying installing the update? I remember we fixing some bugs in this area in this release.

    Here's the link to the update: http://blogs.msdn.com/b/astoriateam/archive/2010/01/27/data-services-update-for-net-3-5-sp1-available-for-download.aspx

    Once you install the update, this should work - the only thing is AppList will be a complex type instead of an entity type, since for a type to become an entity, it must be exposed via an entity set.

    Hope this helps.

    Thanks

    Pratik


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, October 05, 2010 6:32 PM
    Moderator
  • I tried to install the update and tried again. But the same issue occurs. when I press F5, the data service returns "Error processing request". Any further help?
    Monday, October 25, 2010 3:52 AM
  • Hi,

    What provider do you use for the rest of the data service? If it's EF, then I don't think this can work as the data service loads the metadata solely from the EF's CSDL file, and so if the type is not defined there, you can't use it.

    Thanks,


    Vitek Karas [MSFT]
    Monday, October 25, 2010 7:30 AM
    Moderator
  • I have an edmx file that acts as a provider of data. In order to return custom class, can't I map m custom class with a custom entity defined in my EF edmx file? This way the csdl file will have my custom class defined. Thoughts?
    Monday, October 25, 2010 8:33 AM
  • Hi, Have you managed to solve this problem?
    Currently I'm getting the same error... I'm also using an edmx file with wcf...

    Regards,

    Annihil8
    Friday, November 12, 2010 7:55 AM
  • Still struggling. However my workaround was that I created a new project with .net 3.5 framework in VS 2010 and it worked fine. No idea why....?
    Friday, November 12, 2010 8:34 AM
  • Hi Dude,

    Did you solve this issue?

    I got the same problem with you.

    Can you help me?

    I use .NET Framework 4.0 + VS 2010

     

    Thanks in advance!!!!!!

    Monday, July 25, 2011 11:22 AM
  • Same problem

    framework 4 vs 2010

    Wednesday, March 14, 2012 3:24 PM