Working with Custom collection in WCF

No.of Views2867
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  05 Aug 2010 11:08:10
Tag : WCF , General
In this article, i would explorer create custom collection in WCF
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 show you

1.    How to create custom collection
2.    How to use them in WCF Service
3.    How to consume at client side

Before reading this article, I recommend you to read my article Collection in WCF

Create a custom collection

1.    Right click on WCF project and add a new class.  Give any name to this class. I am giving name here MyCustomCollection.
2.    Implement this class form IEnumerable<T>
3.    Serlialize the class by putting Serializable attributes. 

Image Loading

4.    Now add  private list to persist the data 

Image Loading

5.    Now add a Add function to add instance of type T in this list 

Image Loading

6.    Now implement the functions from IEnumerable<T>

Image Loading

So finally MyCustomCollection class will look like below ,

MyCustomCollection.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Collections;
 namespace CRUDOperation { 
[Serializable] public class MyCustomCollection<T> : IEnumerable<T> 
{  private List<T> myList = new List<T>(); 
public void Add(T c)
 {
 myList.Add(c);
 } 
IEnumerator<T> IEnumerable<T>.GetEnumerator() 
{
 return myList.GetEnumerator(); 
} 
System.Collections.IEnumerator IEnumerable.GetEnumerator()
 {
 return myList.GetEnumerator();
 }
 }
 } 

 Creating WCF Service

Now use the custom collection at the contract

IService1.cs

using System;  using System.Collections.Generic;  using System.Linq;  using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web;  using System.Text;
 using System.Runtime.Serialization;
 namespace CRUDOperation 
{ 
[ServiceContract]  public interface IService1 
{ 
[OperationContract]
 MyCustomCollection<Student> GetStudents();
 [OperationContract] void Add(Student s); 
} 
}

 Student.cs

public class Student { public string RollNumber { get; set; } public string Name { get; set; } }

You can see, I am using MyCustomCollection as return type.
Implement the service as

Service1.svc.cs

using System;  using System.Collections.Generic;
 using System.Linq;
 using System.Runtime.Serialization;
 using System.ServiceModel;
 using System.ServiceModel.Web; 
using System.Text;
 namespace CRUDOperation {  public class Service1 : IService1
 { 
MyCustomCollection<Student> lstStudent = new MyCustomCollection<Student>();
 public MyCustomCollection<Student> GetStudents()
 { 
return lstStudent; 
}
 public void Add(Student s) { 
lstStudent.Add(s); 
}
 }
 } 

Consuming at the client

Now the custom collection will also be consumed exactly as the normal concrete collection or IEnumerable interface.  Just add the service reference of the WCF Service in console client.
Custom Collection will be also exposed as array in service Meta data. 

Image Loading

You can see above that custom collection is exposed as array in service Meta data.
So, custom collection can also be consumed as below code
Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1; 

namespace ConsoleApplication1
{class Program
    {static void Main(string[] args)
        {
            Service1Client proxy = new Service1Client();
           
            proxy.Add(new Student { RollNumber = "1", Name = "DJ" });
            Student[]  lstStudent = proxy.GetStudents();foreach (var r in lstStudent)
            {
                Console.WriteLine(r.Name);
            }
            Console.Read();
        }
    }
}

You can see above that I am retrieving the result in array of Student.

Conclusion

In this article, I showed you how to work with custom collection in WCF. I hope this article was useful. 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
</