Why does my WCF service return and ARRAY instead of a List <T>?
Asked Answered
H

3

13

In the web servce I say

 public List<Customer> GetCustomers()
    {
        PR1Entities dc = new PR1Entities();
        var q = (from x in dc.Customers
                select x).ToList();
        return q;
    }

(customer is a entity object)

Then I generate the proxy when I add the service.. and in the reference.cd it say

public wcf1.ServiceReference1.Customer[] GetCustomers() {
        return base.Channel.GetCustomers();
    }

WHY IS IT AN ARRAY? I asked for a List.

help.

Heteroousian answered 26/3, 2010 at 17:43 Comment(0)
C
32

Right click on the service reference and select Configure Service Reference.

In the Collection Type drop-down, select the type System.Collections.Generic.List.

I believe the reason it defaults to Array is that it is the most compatible when serializing. If you're consuming the service from something that recognizes something more complex, you can configure as I mentioned.

Camelback answered 26/3, 2010 at 17:49 Comment(2)
Interesting. Do things work exactly the same when using silverlight instead of ASP.NET ?Heteroousian
I'm not sure what you mean by exactly the same, but I've been able to fully configure this type of casting in Silverlight without issue. By default, Silverlight will convert Arrays to ObservableCollections to take advantage of the advanced binding capabilities. In short, I believe the answer you're looking for is, yes!Camelback
B
4

Right click on your service reference --> Configure Service Reference --> Under "Data Type" change "Collection Type" to System.Collections.ArrayList or whatever type you want the array to deserialize as.

Your list is serialized into an array (server side). You choose how to deserialize it (client side).

Bath answered 26/3, 2010 at 17:48 Comment(0)
B
1

Because that's how a list serialises. Your Customer class on the client side has been generated from the service metadata, which effectively describes how it's serialised not how it was originally defined. If your original Customer class is available, you can specify to re-use it in the client code when you generate the service reference, and then it will come through as a List<T>.

Barncard answered 26/3, 2010 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.