Custom Collection Parameter in a Web Service

This post below will show you how to pass a custom collection as a parameter to the webservice.
Create a custom collection class...
public class Person
{
public int Age;
public string FName, LName;
public Person() { }
 
public Person(string fname, string lname, int age)
{
Age = age;
FName = fname;
LName = lname;
}
}
 
Create a Web Method...
 
[WebMethod]
public void Method(Person[] fields, bool attachmentincluded)
{
           System.Collections.Generic.List<Person> people = new List<Person>(fields);
            foreach (Person i in people)
            {
                string F, L;
                int A;
                F =i.FName;
                L = i.LName;
                A = i.Age;
            }
 }
 
In the consumer application...
 
     //Create People...
     localhost.Person john = new localhost.Person();
     john.FName = "John";
     john.LName = "Doe";
     john.Age = 37;
    
     localhost.Person jane = new localhost.Person();
     jane.FName = "Jane";
     jane.LName = "Doe";
     jane.Age = 37;
    
     //Create array
     localhost.Person[] people = { john, jane };
 
     //Call Web Service
     localhost.Service1 s = new localhost.Service1();
     s.Method(people, false);
 

 

Print | posted @ Thursday, January 24, 2008 8:13 AM

Comments on this entry:

Gravatar # re: Custom Collection Parameter in a Web Service
by Hakan Kutluay at 6/23/2009 1:42 AM

You have to define person class in web server project. When you add web service reference to your client, client side will have same class..
Gravatar # re: Custom Collection Parameter in a Web Service
by Nancy Alcorn at 1/6/2012 11:24 PM

The training kit is very helpful. It contains some basic information regarding Visual Studio 11. The software is indeed advanced and many are using this because of its practicality and advanced features.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: