2012-04-14 80 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