locked
consuming webservice as a collection in c# RRS feed

  • Question

  • User-95657691 posted

    Hi All,

    Our requirement is consume web service which returns list of objects, but need to know how to consume that as a collection  in c# class so that collection can be traversed 

    webservice class looks like this:

    public class EmployeeList : System.Web.Services.WebService
    {

    public EmployeeList()
    {

    //Uncomment the following line if using designed components
    //InitializeComponent();
    }

    [WebMethod]
    public List<Employee> GetEmployees()
    {
    string nwConn = System.Configuration.ConfigurationManager.ConnectionStrings["SqlConnectionString1"].ConnectionString;
    var empList = new List<Employee>();
    using (SqlConnection conn = new SqlConnection(nwConn))
    {
    const string sql = @"SELECT TOP 10 FirstName, LastName, ID FROM Employees";
    conn.Open();
    using (SqlCommand cmd = new SqlCommand(sql, conn))
    {
    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    if (dr != null)
    while (dr.Read())
    {
    var emp = new Employee
    {
    FirstName = dr.GetString(0),
    LastName = dr.GetString(1),
    ID = dr.GetString(2),

    };
    empList.Add(emp);
    }
    return empList;
    }
    }
    }

    }

    public class Employee
    {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ID { get; set; }

    }}

    I want to consume the response  in my client c# class something like below so that i can traverse the collection and populate the classes in client side..

    I tried to do something like this 

    public class clientEmployee
    {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ID { get; set; }

    }

    localhostService.WebService objwebService = new localhostService.WebService();

    List<clientEmployee> obj = new List<clientEmployee>();

    obj= objwebService.GetEmployees()  //** Here Im getting error *****Employee[]**** cannot be assigned be assigned to variable type lis<****clientEmployee***>

    Any solutions will be helpful

    Wednesday, July 9, 2014 10:17 AM

All replies

  • User-760709272 posted
    List<clientEmployee> obj; // you don't need the "new"
     
    obj= objwebService.GetEmployees().ToList();
    

    Wednesday, July 9, 2014 10:24 AM
  • User-95657691 posted

    Thanks for the reply. I have changed it to

    Line 1          List<Project.Employee> objlist;

    Line 2           objlist = objSvc.GetEmployees().ToList();

    Its returning values.

    In Line1 I have changed "clientEmployee" class with actual classname in the webservice "Project.Employee"

    Our requirement is populating list of clientEmployee objects with the webservice response which is list of Project.Employee objects and structure of classes are same
    what will be the efficient way to do that ....

    Also I would like to know how can use the  GetEmployees() method which returns xmlDocuemtnt instead of List<Project.Employee> and how can populate
    the List<clientEmployee> with the response  xmlDocument

    Answers for these to will be helpful

    Wednesday, July 9, 2014 10:54 AM
  • User-2012072944 posted

    Instead of serializing service response in XML format and then parsing that xml to populate custom collection, you can use AutoMapper codeplex library to do the same with less code.

    Here is the example of the same from code project.

    Thanks,

    Abidali

    Thursday, July 10, 2014 3:34 AM
  • User-95657691 posted

    Thanks Abidali ..thats quite handy tool ...

    Our challenge is we need to expose our data to the customers . Current customer  is using Widows forum client.

    We are thinking over what will be the best method to expose or creating the service the data customer need 

    I mean 1) Webservice soap,xml,json  2) WCF returning soap,xml,jason 3) WCF REST with jason,xml 4)WEP api ???

    Any particular suggestion will be helpful ...

    Friday, July 11, 2014 9:19 AM