WCF RESTful Service      Interview Questions         WCF Bindings             WCF Tracing                              

Friday, May 17, 2013

How to return a List from WCF Service?



Consider a WCF service method that returns a generic list as follows:

public List<Employee> 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<Employees> 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<Employee> 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<Employees> employeeList = client.GetAllEmployees();

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


Be social and share with others via:


.NET Jobs from Indeed
Reader of this article might also be interested in:

By Imran Abdul Ghani
Software Developer (Senior)
United Arab Emirates
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.

1 comment: