【问题标题】:Passing Json as stream to WCF method将 Json 作为流传递给 WCF 方法
【发布时间】:2013-04-25 07:57:55
【问题描述】:

我使用 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

问候, 阿西夫·哈米德

【问题讨论】:

  • 如果我理解,您想在您的方法中获取 JSON 流作为 JSON 对象吗?然后你必须实现数据合约。
  • yes json 作为 application/json 类型的流,具有上述服务方法的签名
  • @mtsiakiris:感谢您的回复。我检查了链接,但它没有将 json 发布到任何具有 Stream 参数的方法。
  • 您必须创建自己的 Web 方法,以字符串形式接收数据并对其进行操作以创建对象。 [WebMethod] public string GetData(string JsonRequest){ .... }

标签: c# asp.net wcf


【解决方案1】:

我不确定您是同步执行此操作还是同步执行此操作,但这是传递参数的方法。实现可能会根据客户的需求而有所不同

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

【讨论】:

    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2015-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多