views:

60

answers:

3

How is it possible to return some kind of list from a WCF service, this the method in my WCF service.

My interface:

[OperationContract]
[WebInvoke(Method = "GET",
 ResponseFormat = WebMessageFormat.Json,
 BodyStyle = WebMessageBodyStyle.Wrapped,
 UriTemplate = "Locations")]
IList<Location> GetLocations();

public IList<Location> GetLocations()
{
   Pazar.Data.Repositories.LocationRepository locRepository = 
       new Pazar.Data.Repositories.LocationRepository();
   return locRepository.GetRootLocations().ToList<Location>();
 }

and this how my GetRootLocations looks like, It returns IQueryable, I wonder if I can maybe return IQueryable from my WCF service?

public IQueryable<Location> GetRootLocations()
{
   IQueryable<Location> locations = GetAll().Where(p => !p.ID_Parent.HasValue).OrderBy(p => p.Sequence);
   return locations;
}
A: 

it is a Serliziation problem

Troj
A: 

There is no way to return a List from WCF as this is a .NET specific type. WCF is designed to be consumed by any client, not just .NET clients. To get a List on the client, you must take the array that comes across the wire and create a List in your client side code.

DaveB
A: 

A List is serialized the same way as an Array when it is transferred as a SOAP packet - it is just XML. It's up to your client to determine that the collection should be put into a List instead of an Array.

If you're consuming the service with a .NET client (and using the 'Add Service Reference...' tool), this is very easy. On the Add Service Reference popup click 'Advanced' (or if you already have a Service Reference, right click on it and select 'Configure Service Reference...') and you'll see the configuration screen for the Service Reference.

There's a drop down here that allow you to select a 'Collection type', where the default is System.Array. Just change this to System.Collections.Generic.List and you're done. I typically do this whenever I add a service reference in this way.

Kirk Broadhurst