Objective
In this article, I will show you
- How to create custom collection
- How to use them in WCF Service
- How to consume at client side
Create a custom collection
- Right click on WCF project and add a new class. Give any name to this class. I am giving name here MyCustomCollection.
- Implement this class form IEnumerable<T>
- Serialize the class by putting Serializable attributes.
- Now add private list to persist the data
- Now add a Add function to add instance of type T in this list
- Now implement the functions from IEnumerable<T>
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.
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.