Working with Custom collection in WCF


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. Serialize the class by putting Serializable attributes.

    1.gif
  4. Now add  private list to persist the data 

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

    3.gif
  6. Now implement the functions from IEnumerable<T>

    4.gif
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. 

5.gif
 
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. 

Up Next
    Ebook Download
    View all
    Learn
    View all