2012-04-14 8 views
2

私は、コードは次のようになり、私のクライアントからの404エラーを返すPUTリクエストを持っている:WCF Webサービスへの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およびサービスは、次のようになります。あなたは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では、 'using'が誤ってreqStream ...を2回終了して一般的に見た目をよくしています。 –

+0

あなたの権利ではありますが、私はちょうど投稿をテストし、置いて、取得し、残りの部分を削除します。今のところ4のうち3つ:)ただ今置く必要があります。 –

+0

これについての入札者はありませんか? –

答えて

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); 

をそれ以外の場合はあなただけのXMLにクライアント上で、あなたの学生のオブジェクトをシリアル化でき、内部の要求に沿ってそれを送りますリクエストの本文この場合は、単にhttp://localhost:8000/Service/Studentへのリクエストを行い、WCFは受信リクエストの本文を対応するStudentオブジェクトに逆シリアル化します。

私はそれが、私は学生のコレクションをdelcareことができ、入力文字列studentIDを取ることができるように私の運転の契約を変更する必要がありました:

+0

あなたはUpdateStudentで何をしますか?私が文字列を使用すると、これを達成できません。 'findStudent.FirstName = student.FirstName;' –

+0

私の最初の提案では、姓、名、およびIDはサービスメソッドとは別の文字列パラメータとして入力されます。他のメソッドのパラメータと同じように使用してください。私の2番目の提案は、実際にはリクエストのボディをStudentオブジェクトに逆シリアル化し、そのまま使用することができます。 – KodeKreachor

+0

サービスが特定のインターフェイスを実装している場合、Studentオブジェクトの代わりに別の文字列パラメータを使用するように、インターフェイスのUpdateStudentメソッドのシグネチャを更新する必要があります。あなたのサービスインタフェースを変更したくない場合は、私の最初の提案はあなたのためには機能しません、私の2番目の提案を使用する必要があります。 – KodeKreachor

1

だから私の質問に答えるために、私は2つのことをしなければなりませんでした。

[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

忘れて、私の以前の答えは以下のとおりです。私はdownowotesのためにあなたのために働いていないと思ったので、私はそれを削除しました。あなたはそれが働いていることを聞いてよかった – KodeKreachor

関連する問題