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


Top 5 Contributors of the Month
david stephan

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

 Subscribe to Articles

Custom collection in WCF Tutorial

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

In this Tutorial, I will show you how to create custom collection, use them in WCF Service and how to consume at client side like How to create custom collection, How to use them in WCF Service etc
 

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 
  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

    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 :  IEnumerable
   {
       private List myList = new List();
       public void Add(T c)
       {
           myList.Add(c);
       }
       IEnumerator IEnumerable.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 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 lstStudent = new MyCustomCollection();
       public MyCustomCollection 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.

 Subscribe to Articles

     

Further Readings:

Responses

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