tags:

views:

186

answers:

4

I have a WCF service method which I have written for return type as collection type.

But if I generate a proxy class and consume the method from the client the methods return type is changed from a collection to an array.

Why is this happening?

+1  A: 

You need to tell svcutil to generate the types as whatever generic type you want. You do this by adding this switch to the svcutil command:

svcutil ..blah... /ct:System.Collections.ObjectModel.Collection`1

The /ct is short for /collectionType. In this case I specified Collection<T> but you could have just as easily used System.Collections.Generic.List<T>.

Andrew Hare
+1  A: 

If you generate the proxy using "Add Service Reference" in Visual Studio there is an "Advanced" button that brings up a dialog that allows you to specify what the return type should be (including Generic types).

Jimmie R. Houts
A: 

If you're using Visual Studio you can just right click your service reference to configure it. You'll then have an option (in advanced settings?) to tell your app how to handle arrays as collections.

vidalsasoon
+4  A: 

Remember - WCF is also an interoperable system, e.g. your other end of the wire could be a PHP or Java or Ruby client which will not be able to understand the .NET generic list!

You can specify you want generic lists with the two options Andrew and Jimmie have mentioned - this works, if and only if you also use the DataContractSerializer (which is the default choice for WCF). However, if your service and/or data contract for whatever reason needs to use the XmlSerializer instead, then these settings won't help - your lists will be turned into arrays once again.

So try these options shown, and if they work for you - great! But be aware that there are good reasons why your generic list might just have to be turned into a more interoperable array of objects.

Marc

marc_s
+1 Nice explanation.
Andrew Hare