2012-01-12 80 views
0

這裏是我試圖從我的WCF服務調用DELETE方法:呼叫從客戶端應用程序了刪除WCF RESTful服務的方法

string tmpUrl1 = "http://localhost:1234/MyService.svc/EndPoint/MyMethod"; 
WebRequest request1 = WebRequest.Create(tmpUrl1); 
request1.Method = "DELETE"; 
byte[] byteArray1 = Encoding.UTF8.GetBytes("{\"idName\":" + newIdName + "}"); 
request1.ContentType = "application/json"; 
request1.ContentLength = byteArray1.Length; 
Stream dataStream1 = request1.GetRequestStream(); 
dataStream1.Write(byteArray1, 0, byteArray1.Length); 
dataStream1.Close(); 
WebResponse response1 = request1.GetResponse(); 

,但我得到錯誤400

這裏是方法的名稱在wcf:

[OperationContract] 
    [WebInvoke(
     Method = "DELETE", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     UriTemplate = "/MyMethod/{deleteRP}/", 
     BodyStyle = WebMessageBodyStyle.Bare 
       )] 
    MyClass MyMethod(string deleteRP); 

我在哪裏犯了一個錯誤?

回答

0

嘗試對您的服務啓用Tracing並檢查跟蹤日誌中是否存在實際錯誤。此外,您的URL地址應該是這樣

"http://localhost:1234/MyService.svc/EndPoint/MyMethod/55" 

而不是

的 「http://本地主機:1234/MyService.svc /端點/的MyMethod」

UPDATE:

private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody) 
     { 
      byte[] bytes = null; 
      var serializer1 = new DataContractSerializer(typeof(T)); 
      var ms1 = new MemoryStream(); 
      serializer1.WriteObject(ms1, requestBody); 
      ms1.Position = 0; 
      var reader = new StreamReader(ms1); 
      bytes = ms1.ToArray(); 
      return bytes; 
     } 

現在換下面的線

byte[] byteArray1 = Encoding.UTF8.GetBytes("{\"idName\":" + newIdName + "}"); 

byte[] array = ToByteArrayUsingDataContractSer<string>("{\"idName\":" + newIdName + "}"); 
+0

好byteArray1是給的鏈接 – 2012-01-12 12:48:28

+0

我猜「55」的部分有當其試圖反序列化字節組被張貼在服務器上的問題。請找到序列化請求的代碼。希望能幫到你 – Rajesh 2012-01-12 13:09:02

+0

我使用相同的代碼來調用POST方法(而不是DELETE,我把POST)並且一切正常。所以不是這樣。 – 2012-01-12 13:11:50