|
WCF service method which returns generic List gets converted into Array when consumed in the client.
I had created a method which was returning List in WCF service and when i tried to get the returned value in List I got below error.
Cannot implicitly convert type 'EmployeeClient.EmployeeServiceReference.Employee[]' to 'System.Collections.Generic.List<EmployeeClient.EmployeeServiceReference.Employee>'
Let's see how we can resolve the issue.
Step 1: Add new project (WCF Service Library) with name EmployeeService
Step 2: Create Employee class with EmpId, FirstName, LastName, Address, Age and Designation data members
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization;
namespace EmployeeService { [DataContract] public class Employee { [DataMember] public int EmpId; [DataMember] public string FirstName; [DataMember] public string LastName; [DataMember] public string Address; [DataMember] public int Age; [DataMember] public string Designation; } }
Step 3: Now create IEmployeeService class with SaveEmployee, DeleteEmployee and GetLstEmployees and GetArrEmployees methods. GetLstEmployees method returns List<Employee> and GetArrEmployees method returns Employee[].
using System.Collections.Generic; using System.ServiceModel; namespace EmployeeService { [ServiceContract] public interface IEmployeeService { [OperationContract] void SaveEmployee(Employee emp); [OperationContract] void DeleteEmployee(int empId); [OperationContract] List<Employee> GetLstEmployees(); [OperationContract] Employee[] GetArrEmployees(); } }
Step 4: Create EmployeeService class which will inherit IEmployeeService
using System.Collections.Generic; using System.ServiceModel; namespace EmployeeService { [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
Public class EmployeeService:IEmployeeService { #region IEmployeeService Members List<Employee> lstEmp = new List<Employee>(); public void SaveEmployee(Employee emp) { lstEmp.Add(emp); }
public void DeleteEmployee(int empId) { lstEmp.Remove(lstEmp.Find( x => x.EmpId.Equals(empId))); }
public List<Employee> GetLstEmployees() { return lstEmp; }
public Employee[] GetArrEmployees() { Employee[] arrEmp = lstEmp.ToArray(); return arrEmp; }
#endregion } }
WCF service is ready to be consumed by client application.
Step 5: In client application, Add Service Reference. Provide Address and Namespace. Click on Advance button. Service Reference Setting window will open, Select System.Collections.Generic.List as Collection Type and click on OK. Now click OK of Add Service Reference window. WCF service is ready to be used in the client application.
If WCF reference is already added Service Reference Setting can be changed by Update Service Reference.

Step 6: Add three button in client web applicaiton. I am using web application as client for this article, one can use windows or console application also.
<asp:Button ID="btnAdd" runat="server" onclick="btnAdd_Click" Text="Save" /> <asp:Button ID="btnGetDataArray" runat="server" onclick="btnGetDataArray_Click" Text="Get Data using Array" /> <asp:Button ID="btnGetDataList" runat="server" onclick="btnGetDataList_Click" Text="Get Data using List" /> <asp:Label ID="lblEmpNo" runat="server" Text="No. of Employees: "></asp:Label>
Step 7: Now add btnAdd_Click method to save data, btnGetDataList_Click method to get data as List<Employee> and btnGetDataArray_Click to get Employee array.
protected void btnAdd_Click(object sender, EventArgs e) { EmployeeServiceClient empClient = new EmployeeServiceClient("WSHttpBinding_IEmployeeService"); Employee objEmp = new Employee(); Random rnd = new Random(); int intRnd = rnd.Next(); objEmp.EmpId = intRnd; objEmp.FirstName = "ABC" + intRnd.ToString(); objEmp.LastName = "XYZ" + intRnd.ToString(); objEmp.Address = "Add" + intRnd.ToString(); objEmp.Designation = "S/W Engg"; objEmp.Age = 21; empClient.SaveEmployee(objEmp); }
protected void btnGetDataArray_Click(object sender, EventArgs e) { EmployeeServiceClient empClient = new EmployeeServiceClient("WSHttpBinding_IEmployeeService"); List<Employee> lstEmp = empClient.GetLstEmployees(); lblEmpNo.Text = lstEmp.Count; }
protected void btnGetDataList_Click(object sender, EventArgs e) { EmployeeServiceClient empClient = new EmployeeServiceClient("WSHttpBinding_IEmployeeService"); List<Employee> lstEmp = empClient.GetArrEmployees(); lblEmpNo.Text = lstEmp.Count; }
Now, btnGetDataArray_Click method retuns Employee[] in WCF service but while using in client it returns List<Employee> lstEmp.
|