将Json作为流传递给WCF方法

时间:2013-04-25 07:57:55

标签: c# asp.net wcf

我使用WCF REST服务模板40(CS)创建了一个网站,它有一个这样的服务方法:

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

public string CTNotification(Stream contents)

我怎样才能将json传递给它?我不能将服务参数作为字符串,因为客户端将发送json作为流。如何使用C#对内容类型为= application / json

的此服务方法进行调用

此致 Asif Hameed

1 个答案:

答案 0 :(得分:0)

我不确定你是在同步还是同步,但这里是如何传递你的args。实施可能会根据客户的需求而有所不同

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