How to configure a WCF proxy to return a generic list

In this post I will explain to you how you can configure your WCF proxy client to return a generic list as a collection type instead of an array. When you specify in your service contract that an operation should return a generic list, WCF will by default send your collection over the wire as an array.

If WCF sends this to the client you will lose the point of trying to pass a generic collection to your client. So let’s say you have the following operation:

   1: [OperationContract]
   2: ICollection<Material> GetMaterials(string warehouseCode);

Now if you are going to consume the service and try to call the method that we just created you will see that you will not receive your generic collection. Instead you will just receive an array of Materials.

image

You can actually let the WCF service return all sort of collection types, it’s just a matter of configuration. So let’s properly configure it!

Right mouse click on the service reference that we created and select “Configure Service Reference”. Here you will immediately see that you can specify something for the Collection type and the Directory collection type. That’s right…here we can set the correct collection type that the service should return. The directory collection type can also be set here. Let’s configure it to return a generic list.

image

Now when you hover over the function call, you will see the following:

image

If you want to use the svcutil.exe to generate your proxy class, you need to use the /collectionType (/ct) switch to specify the correct collection.

For more details check out the ServiceModel MetaData Utility Tool on MSDN library


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word