WCF/REST Something is up with my POST?

Go To StackoverFlow.com

1

When I POST using my windows form app (second peace of code down) when I go to GET the student collection with buttonclick 2 into my datagrid nothing is showing I can hardcode the members and GET no problem, yet I cant POST? The response I get back says OK in the message box when I click button1 for my post? So not quite sure what I have done...

    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "")]
    void AddStudent(Student student);

            XDocument xDoc = XDocument.Load(uri);
            var students = xDoc.Descendants("Student")
                .Select(n => new
                {
                    StudentNo = n.Element("StudentID").Value,
                    Firstname = n.Element("FirstName").Value,
                    Surname = n.Element("LastName").Value
                })
                .ToList();

            dataGridView1.DataSource = students;
        }
2012-04-05 20:07
by Garrith Graham
Be consistent with naming StudentID or StudentNo, FirstName or Firstname, LastName or SurnameL.B 2012-04-05 20:14
hey Denis this is purely for visual representation to the use - Garrith Graham 2012-04-05 20:17
Is the var students populated? If so, try using BindingContextAnurag Ranjhan 2012-04-05 20:20
That method doesnt work still the same return of the 3 students. Im not sure if its my addstudent in my operationcontract thats doing it - Garrith Graham 2012-04-05 20:26
In your similar question I see that your xml's root element is ArrayOfStudent. Why do you form your xml manually instead of using an xml parser? what is the use of Student class if you don't use it for serializing/deserializing - L.B 2012-04-05 20:29
I dont follow denis - Garrith Graham 2012-04-05 20:33
Are you sure that you form your xml correctly? Maybe it should have a root tag as ArrayOfStudent not StudentL.B 2012-04-05 20:35
Using string builder to form an xml is a bad idea. Use Xml Parsers like XmlDocument, XDocument etc - L.B 2012-04-05 20:38
You can also use XmlSerializer to serialize your Student class to get the xml to post to server - L.B 2012-04-05 20:39


1

Are you running in Per Call Activation mode? If so, every client request gets a new dedicated service instance, so your List<> is being recreated as empty every time.

See this reference article. You'll have to persist your List between calls, either in a Cache or Database.

2012-04-06 01:24
by mgnoonan
GOD! That was so much reading to find the answer which ofcourse I knew it would be a one line sort of thing: [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]Garrith Graham 2012-04-06 01:57


0

Your AddStudent is not adding to the list

public void AddStudent(Student student)
{
    student.StudentID.ToString();
    student.FirstName.ToString();
    student.LastName.ToString();
}

And GetStudentCollection is returning the same hard coded values.

static List<Student> students = new List<Student>();
public void AddStudent(Student student)
{
   students.Add(student);

}

public List<Student> GetStudentCollection()
{
    return students;
}
2012-04-05 20:28
by Anurag Ranjhan
still nothing returned, no errors either? Btw I had this originally but I also wanted the hardcoded members (for easeness - Garrith Graham 2012-04-05 20:43
Then there may be other issues also. Your Addstudent is probably not working correctly. Can you put a breakpoint there and see if is being called with a properly formatted student - Anurag Ranjhan 2012-04-05 20:49


0

Your service is doing exactly what you wrote in code. ListStudents constantly returns the same list of the students, AddStudent do nothing with the list.

2012-04-05 20:37
by paramosh
Ads