2012-01-12 82 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