2012-04-14 78 views
2

我是從我的客戶端返回404錯誤PUT請求,代碼如下所示:PUT方法到WCF Web服務的「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的和服務是這樣的:基於URI你

[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 
+1

FWIW,'使用'會意外地關閉reqStream ...兩次......並使代碼看起來更乾淨一般。 – 2012-04-14 17:51:06

+0

你的權利,但即時通訊只是測試的職位,放,得到,在休息刪除。到目前爲止3滿分4 :)現在只需要投入。 – 2012-04-14 17:54:15

+0

對此沒有接受者? – 2012-04-14 18:24:39

回答

0

打電話,你的服務是否能夠解決它給予額外的路由信息​​傳遞給它?

你可以嘗試更新服務方法簽名接受和映射ncoming 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對象。

+0

你會在UpdateStudent中做什麼?如果我使用了一個字符串,我無法實現這個'findStudent.FirstName = student.FirstName;' – 2012-04-14 19:44:48

+0

在我的第一個建議中,名字,姓氏和id都將作爲單獨的字符串參數傳入您的服務方法,所以您可以然後像通常其他任何方法參數那樣使用它們。我的第二個建議實際上是將請求的主體反序列化爲你的Student對象,然後你可以像現在一樣使用它。 – KodeKreachor 2012-04-14 19:55:25

+0

如果您的服務正在實現特定的接口,那麼您必須更新接口中的UpdateStudent方法簽名,以使用單獨的字符串參數而不是Student對象。如果你不想改變你的服務界面,那麼我的第一個建議不適合你,你不得不使用我的第二個建議。 – KodeKreachor 2012-04-14 20:10:04

1

所以爲了回答我的問題,我不得不做兩件事情:

我不得不改變我的經營合同,以便它可以把輸入字符串studentID,然後我可以delcare學生集合。

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

有一個人在此之前已經提出了答案,他是正確的,所以我想感謝你,你的答案將被接受! (如果它沒有被刪除)

+2

很高興爲你工作,我早先的答案如下。我認爲這不是爲你工作,因爲downvotes所以我刪除它。很高興聽到你的工作 – KodeKreachor 2012-04-15 04:03:21