consider following code..
[Serializable]
public class Student
{
private string studentName;
private double gpa;
public Student()
{
}
public string StudentName
{
get{return this.studentName;}
set { this.studentName = value; }
}
public double GPA
{
get { return this.gpa; }
set { this.gpa = value; }
}
}
private ArrayList开发者_开发知识库 studentList = new ArrayList();
[WebMethod]
public void AddStudent(Student student)
{
studentList.Add(student);
}
[WebMethod]
public ArrayList GetStudent()
{
return studentList;
}
I want to consume that web service using simple C# client form application. I can't get the student list using following code segment..
MyServiceRef.Student student = new Consuming_WS.MyServiceRef.Student();
MyServiceRef.Service1SoapClient client = new Consuming_WS.MyServiceRef.Service1SoapClient();
any idea..??
Thank in advance!
The problem is that your web service is not stateless. Each time the web service is called, a new instance of your web service class is instanced and the method is called on this instance. When the instance is called, studentList is assigned a new empty list.
You need to alter you state management. E.g.
private static ArrayList studentList = new ArrayList();
would probably work better but it is still not reliable. Check the article at http://www.beansoftware.com/asp.net-tutorials/managing-state-web-service.aspx for a sample that stores the state in Session (or Application).
Edit: added sample code to avoid usage of ArrayList.
To avoid problems with ArrayList and ArrayOfAnyType:
private List<Student> studentList = new List<Student>();
[WebMethod]
public void AddStudent(Student student)
{
studentList.Add(student);
}
[WebMethod]
public Student[] GetStudent()
{
return studentList.ToArray();
}
精彩评论