2013-04-25 22 views
0

我創建使用WCF REST服務模板40(CS),它有這樣的服務方法的網站:傳遞JSON作爲流WCF方法

[WebInvoke(UriTemplate = "CTNotification", Method = "POST", ResponseFormat = WebMessageFormat.Json, 
      RequestFormat = WebMessageFormat.Json)]  

public string CTNotification(Stream contents) 

我如何通過JSON呢?我無法將服務參數設置爲字符串,因爲客戶端將發送json作爲流。我怎樣才能讓一個POST調用使用C#與內容類型=應用程序/ JSON的這種服務方法

問候, 阿西夫·哈米德

+0

如果我明白了,您希望在您的方法中將JSON流視爲JSON對象?然後您必須實施數據合同。 – mtsiakiris 2013-04-25 08:11:55

+0

是json作爲類型application/json的流類型,帶有以上服務方法的簽名 – DotnetSparrow 2013-04-25 08:14:56

+0

看看這個:http://www.codeproject.com/Articles/327420/WCF-REST-Service-with-JSON – mtsiakiris 2013-04-25 08:16:09

回答

0

我不知道,如果你這樣做是同步或同步,但這裏是如何通過你的參數。實施可能會根據客戶的需求而有所不同

HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(ServiceAddress + "CTNotification/"); 

       //parameters 
       byte[] data = Encoding.UTF8.GetBytes(jsonString); 

       httpWReq.Method = "POST"; 
       httpWReq.ContentType = "application/x-www-form-urlencoded"; 
       httpWReq.ContentLength = data.Length; 
       httpWReq.Timeout = 1000; 

       using (Stream newStream = httpWReq.GetRequestStream()) 
       { 
        newStream.Write(data, 0, data.Length); 
       }