I have a PUT request which is returning a 404 error from my client, the code looks like this:
{
string uriupdatestudent = string.Format("http://localhost:8000/Service/Student/{0}/{1}/{2}", textBox16.Text, textBox17.Text, textBox18.Text);
byte[] arr = Encoding.UTF8.GetBytes(uriupdatestudent);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriupdatestudent);
req.Method = "PUT";
req.ContentType = "application/xml";
req.ContentLength = arr.Length;
using (Stream reqStrm = req.GetRequestStream())
{
reqStrm.Write(arr, 0, arr.Length);
reqStrm.Close();
}
using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
{
MessageBox.Show(resp.StatusDescription);
resp.Close();
}
}
The OperationContract and Service looks like this:
[OperationContract]
[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student")]
void UpdateStudent(Student student);
public void UpdateStudent(Student student)
{
var findStudent = students.Where(s => s.StudentID == student.StudentID).FirstOrDefault();
if (findStudent != null)
{
findStudent.FirstName = student.FirstName;
findStudent.LastName = student.LastName;
}
}
[DataContract(Name="Student")]
public class Student
{
[DataMember(Name = "StudentID")]
public string StudentID { get; set; }
[DataMember(Name = "FirstName")]
public string FirstName { get; set; }
[DataMember(Name = "LastName")]
public string LastName { get; set; }
[DataMember(Name = "TimeAdded")]
public DateTime TimeAdded;
public string TimeAddedString
Based on the uri you’re calling, is your service able to resolve it given the extra routing information being passed to it?
You could try updating the service method signature to accept and map the ncoming uri parameters:
Otherwise you could just serialize your Student object on the client into XML and send it along with the request inside the body of the request. In this case you would simply make a request to: http://localhost:8000/Service/Student and WCF would deserialize the incoming request’s body to a corresponding Student object.