c# - WCF - 发布 JSON 对象

标签 c# json wcf

我正在尝试将 JSON 发布到 WCF 服务。 json 对象包含一个数组。我想知道如何正确绑定(bind)到我的数据契约(Contract)。如果有人能在这里给我指点,我将不胜感激。目前我的购物车对象为空

这是我的服务界面的样子:

public interface IService
 {

[OperationContract]
 [WebInvoke(UriTemplate = "/cart", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
 Ship GetShipInfo( Cart cart, string Website);
 }

[DataContract]
 public class Cart
 {
 [DataMember]
 public Int32 ProductID { get; set;}
 [DataMember]
 public decimal ItemPrice { get; set; }
 [DataMember]
 public Int16 Qty { get; set; }
 [DataMember]
 public String SizeWidth { get; set; }
 }

我的Client调用如下

客户电话

using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.UI;
 using System.Web.UI.WebControls;
 using System.Runtime.Serialization.Json;
 using System.Net;
 using System.IO;

public partial class _Default : System.Web.UI.Page
 {
 protected void Page_Load(object sender, EventArgs e)
 {

DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
 Cart cart = new Cart{ ProductID = 1000, ItemPrice = Convert.ToDecimal(32.50), Qty = 1, SizeWidth = “6M” };
 WebClient Proxy1 = new WebClient();
 Proxy1.Headers["Content-type"] = “application/json”;
 MemoryStream ms = new MemoryStream();
 DataContractJsonSerializer serializerToUplaod = new DataContractJsonSerializer(typeof(Cart));
 serializerToUplaod.WriteObject(ms, cart);

 byte[] data = Proxy1.UploadData(“http://localhost:54897/IphoneService.svc/cart”, “POST”, ms.ToArray());
 Stream stream = new MemoryStream(data);
 obj = new DataContractJsonSerializer(typeof(Ship));
 var Ship = obj.ReadObject(stream) as Ship;

}

public class Ship
 {
 public Decimal SecondDay { get; set; }
 public Decimal NextDay { get; set; }
 }

public class Cart
 {

public Int32 ProductID { get; set; }

public Decimal ItemPrice { get; set; }

public Int16 Qty { get; set; }

public String SizeWidth { get; set; }
 }

}

我的 JSON 看起来像这样

{"cart":
[
{"ProductID":2957,
"Qty":1,
"ItemPrice":60,
"SizeWidth":"5M"}
]
}

最佳答案

来自 Fiddler 的 WCF REST 方法的原始请求应如下所示:

POST http://localhost:54897/IphoneService.svc/cart HTTP 1.1
Content-Type: application/json
Host: localhost

{"cart":{"ProductId":1,"ItemPrice":60,"Qty":1,"SizeWidth":"5M"},"Website":"sample website"}

JSON 格式的响应如下所示:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 30

{"SecondDay":5.0, "NextDay":7.0}

关于c# - WCF - 发布 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10654383/

相关文章:

python - 使用 kivy 将加速度计数据存储在 json 文件中时出错。

android - 使用MPAndroidChart单独绘制LineChart?

c# - 允许使用 LINQ 查询中的 ToDictionary() 重复键

c# - 在 WPF 中打开 WebView2 会在调用 EnsureCoreWebView2Async 时导致 System.UnauthorizedAccessException

c# - 如何在 UWP XAML/C# 中更改滚动查看器的滚动量?

c# - Xamarin.Forms JSON 对象到 Listview

java - 如何在json post请求中发送字节数组?

azure - azure 中的证书( *.p12 , *.cert )- webjob

c# - 代理设置设置为 "Use automatic configuration script"的 WCF 客户端

c# - 获取 Azure 服务总线中的消息统计信息