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:

Category: Uncategorized Tags:

About IMRAN ABDUL GHANI

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

One thought on “How to return a List from WCF Service?

Comments are closed.