How to return a List from WCF Service?

By | May 17, 2013
Consider a WCF service method that returns a generic list as follows:
public List GetAllEmployees()
//code details here….

//returning a generic List<Employee>
On Client-side, if we try to access returned value of that web service method as follows:

MyServiceClient client = new MyServiceClient();
List employeeList = client.GetAllEmployees();

We will face following error:
Cannot Implicitly convert type ‘MyService.Employee[]’ to System.Collections.Generic.List<MyService.Employee>.

Actually, Collections are exposed as array in Service metadata. Generic lists from a WCF service method will be returned as arrays, so we need to do some extra steps to get List on client-side.

While adding a 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.

Now following code will run smoothly without any issue.
List employeeList = client.GetAllEmployees();

Hopefully this short WCF Tutorial will resolved the issue specified above.

Top 10 Interview Questions and Answers Series:

  • Anonymous

    Thanks dude, It worked