.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WCF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Collection in WCF Tutorial

Posted By:Dhananjay Kumar       Posted Date: August 01, 2010    Points: 25    Category: WCF    URL: http://www.dotnetspark.com  

Collection in WCF Tutorial. In this we will see How to work with collection in WCF? How to work with concrete collection List ? How to convert Array at client side to collection etc.

Collection in WCF


 In this article, I will explain

1.  How to work with collection in WCF?

2.  How to work with concrete collection List ?

3.  How to work with interface IEnumerable ?

4.  How to convert Array at client side to collection

You can also follow my blog

Collection at service side and client side

Since, COLLECTION is very much specific to .Net, so WCF does not expose it to the metadata of the service.  But they are very useful, so WCF provides dedicated marshaling rules for collection.

Collection will be exposed as array in service metadata

Let us say, you are having a contract and service implementation as below, If you see carefully, I am returning a collection of the type student



Now when you add this service reference at the client side, you can see the collection is exposed as array.  To see this, add the service reference in a client. 
I have created a console client and added the service reference of above service.  When you click on ServiceReference1 in solution explorer, you can open the proxy code generated at the client side.  So click on object browser and you can see that array has been returned in metadata instead of collection.

At the client side

Working with List or Concrete collection

Imagine a scenario, where return type is concrete collection not an interface. For example, we need to return List instead if IEnumerable
As we know from .Net 3.5 all the classes are by default serialized.  And collection we are returning is marked with Serializable not with DataContract.

Signature of Add method should be,

Let us say,
Contract is,


If you see above, we are returning List instead of IEnumerable. So again at the client side, List would be exposed as Student [] in service metadata.

Service implementation is as below,


And again after adding the service reference at the client side, you can see in the object browser List is exposed as Student[]
So, to call the service at the client side, we need to get the student in array of student

So if you see above code, I am taking the output of GetStudents() method in array of student(Student []).
If you want to avoid that, you do not want to use array but instead you want to get collection at the client side.

Follow the below steps,

Step 1

While adding the service reference click on Advanced option

Step 2

From the drop down of collection type tab, select System.Collections.Generic.List  option.

Now at the client side,

Now you can see at the client side proxy is having list as return not array .


I hope this article was useful to you.  Thanks for reading. Happy Coding .

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend