views:

56

answers:

1

hi,

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!

A: 

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();
}
Andreas Paulsson
:: Nop..the problem is still here. It says.."Cannot implicitly convert type 'Consuming_WS.MyServiceRef.ArrayOfAnyType' to 'System.Collections.ArrayList' "...Any idea..??
udayalkonline
See my added sample above to work around this problem. The problem is that you are using ArrayList in web service interface.
Andreas Paulsson