Working with Collection in WCF

No.of Views2476
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  02 Aug 2010 10:08:45
Tag : WCF , Miscellaneous
In this article, I will explain,How to work with collection in WCF and Generic collection as well
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I will explain

1.    How to work with collection in WCF?
2.    How to work with concrete collection List<T> ?
3.    How to work with interface IEnumerable<T> ?
4.    How to convert Array at client side to collection

Collection at service side and client side

Image Loading

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

Image Loading

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

IService1.cs 

Image Loading

Service1.svc.cs 

Image Loading

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 

Image Loading

Working with List<T<> or Concrete collection

Imagine a scenario, where return type is concrete collection not an interface. For example, we need to return List<T> instead if IEnumerable<T>. 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. 

Image Loading

Signature of Add method should be, 

Image Loading

Let us say, Contract is,

IService1.cs 

Image Loading

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

Image Loading

Service implementation is as below,

Service1.svc.cs 

Image Loading

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

Image Loading

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. 

Image Loading

Follow the below steps,

Step 1

While adding the service reference click on Advanced option 

Image Loading

Step 2

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

Image Loading

Now at the client side, 

Image Loading

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

Conclusion

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

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top
</