2

我有一个 PUT 请求,它从我的客户端返回 404 错误,代码如下所示:

    {
        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();
        }
    }

OperationContract 和 Service 如下所示:

    [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
4

2 回答 2

1

所以为了回答我的问题,我必须做两件事:

我必须更改我的操作合同,以便它可以接受输入字符串 studentID,然后我可以删除学生集合。

    [OperationContract]
    [WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student/{studentID}")]
    void UpdateStudent(string studentID, Student student);

    public void UpdateStudent(string studentID, Student student) 
    {
        var findStudent = students.Where(s => s.StudentID == studentID).FirstOrDefault();

        if (findStudent != null)
        {
            findStudent.FirstName = student.FirstName;
            findStudent.LastName = student.LastName;
        }

    }

然后从客户端我不得不返回使用字符串生成器方法以将集合作为 xml 发送。

    {
        string uriupdatestudent = string.Format("http://localhost:8000/Service/Student/{0}", textBox16.Text);
        StringBuilder sb = new StringBuilder();
        sb.Append("<Student>");
        sb.AppendLine("<FirstName>" + this.textBox17.Text + "</FirstName>");
        sb.AppendLine("<LastName>" + this.textBox18.Text + "</LastName>");
        sb.AppendLine("</Student>");
        string NewStudent = sb.ToString();
        byte[] arr = Encoding.UTF8.GetBytes(NewStudent);
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uriupdatestudent);
        req.Method = "PUT";
        req.ContentType = "application/xml";
        req.ContentLength = arr.Length;
        Stream reqStrm = req.GetRequestStream();
        reqStrm.Write(arr, 0, arr.Length);
        reqStrm.Close();
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        MessageBox.Show(resp.StatusDescription);
        reqStrm.Close();
        resp.Close();
    }

有人在此之前提出了答案,他是正确的,所以我要感谢您,您的答案将被接受!(如果它没有被删除)

于 2012-04-14T22:01:51.673 回答
0

根据您调用的 uri,您的服务是否能够在传递给它的额外路由信息的情况下解决它?

您可以尝试更新服务方法签名以接受和映射即将到来的 uri 参数:

[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/Student/{fname}/{lname}/{id}")]
    void UpdateStudent(string fname, string lname, string id);

否则,您可以将客户端上的 Student 对象序列化为 XML,并将其与请求正文中的请求一起发送。在这种情况下,您只需向http://localhost:8000/Service/Student发出请求,WCF 会将传入请求的主体反序列化为相应的 Student 对象。

于 2012-04-14T19:40:18.123 回答