As we know Collection is very much specific to .Net, WCF does not expose it to the metadata of the service. WCF provides marshaling rules for collection.
Actually, Collections are exposed as array in Service metadata.
Let us understand it with the help of an example.
public interface IStudentService
IEnumerable<StudentInformation> GetStudentInfo(int studentId);
public class StudentService : IStudentService
List<StudentInformation> Students = new List<StudentInformation>();
public IEnumerable<StudentInformation> GetStudentInfo(int studentId)
IEnumerable<StudentInformation> Student = from student in Students
where student.StudentId == studentId
public List<StudentInformation> GetStudentData()
List<StudentInformation> Student = (from student in Students
When we add this service reference at the client side, we can see the collection is exposed as array.
On Client-side, if we try to access returned value of the service method
StudentServiceClient objClient = new StudentServiceClient();
List<StudentInformation> studentList = objClient.GetStudentData();
we will face following error:
Cannot Implicitly convert type ‘StudentService.StudentInformation’ to System.Collections.Generic.List<StudentService.StudentInformation>.
Since, we are returning collection from the service but at the client side we are getting array. There is a disparity at the service end (Collection) and client end (Array). To overcome from this issue, we need to change the setting while adding service reference. While adding service reference, click on “Advance” button. It will open “Service Reference Settings” window. On this window, choose appropriate option for Collection Type i.e. System.Collections.Generic.List option.
Now following code will run smoothly without any issue at the client side,
IEnumerable<StudentInformation> objStudent = objClient.GetStudentInfo(Convert.ToInt16(txtStudentId.Text));
List<StudentInformation> lstStudent = objClient.GetStudentData();
I hope this article will be useful to you. Thanks for reading. Happy Coding .