WCF - 发布JSON对象 [英] WCF - Post JSON object

查看:113
本文介绍了WCF - 发布JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想POST JSON到WCF服务。 JSON对象包含一个数组。我不知道如何正确地绑定到我的数据合同。如果任何人都可以给我在这里一个指针我真的很感激它。目前我的购物车对象为空



这是我的服务界面看上去是这样的:

 公共接口IService 
{

[OperationContract的]
[WebInvoke(UriTemplate =/购物车,方法=POST,BodyStyle = WebMessageBodyStyle.Wrapped,RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
船舶GetShipInfo(车车,串网站);
}

[DataContract]
公共类车
{
[数据成员]
公众的Int32的ProductID {搞定;设置;}
[数据成员]
公共小数ITEMPRICE {搞定;组; }
[数据成员]
公共Int16的数量{搞定;组; }
[数据成员]
公共字符串SizeWidth {搞定;组; }
}



我的客户电话如下:



客户端调用

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;使用System.Web.UI.WebControls
;
使用System.Runtime.Serialization.Json;使用System.Net
;
:使用System.IO;

公共部分类_Default:System.Web.UI.Page
{
保护无效的Page_Load(对象发件人,EventArgs五)
{

DataContractJsonSerializer的obj =新DataContractJsonSerializer(typeof运算(字符串));
车车=新的车{产品id = 1000,ITEMPRICE = Convert.ToDecimal(32.50),数量= 1,SizeWidth =6M};
WebClient的Proxy1 =新的WebClient();
Proxy1.Headers [内容类型] =应用/ JSON;
的MemoryStream毫秒​​=新的MemoryStream();
DataContractJsonSerializer serializerToUplaod =新DataContractJsonSerializer(typeof运算(车));
serializerToUplaod.WriteObject(MS,车);

字节[]数据= Proxy1.UploadData(HTTP://本地主机:54897 / IphoneService.svc /购物车,POST,ms.ToArray());
流流=新的MemoryStream(数据);
OBJ =新DataContractJsonSerializer(typeof运算(船舶));
VAR船舶= obj.ReadObject(流)船舶;

}

公共类船舶
{
公共小数SecondDay {搞定;组; }
公共小数NextDay {搞定;组; }
}

公共类车
{

公众的Int32的ProductID {搞定;组; }

公共小数ITEMPRICE {搞定;组; }

公共Int16的数量{搞定;组; }

公共字符串SizeWidth {搞定;组; }
}

}



我的JSON看起来像这样

  {购物车:
[
{产品ID:2957,
数量:1,
ITEMPRICE:60,
SizeWidth:5M}
]
}


解决方案

原始的要求为您WCF REST方法应该从提琴手看起来如下:

  POST HTTP://本地主机:54897 / IphoneService.svc /购物车HTTP 1.1 
内容类型:应用程序/ JSON
主机:本地主机

{购物车:{产品编号:1,ITEMPRICE:60,数量:1,SizeWidth:5M},网站:样品网站}

在JSON的响应看起来如下:

  HTTP / 1.1 200 OK 
内容类型:应用程序/ JSON
的Content-Length:30

{SecondDay:5.0, NextDay:7.0}


I am trying to POST JSON to a WCF service. The json object contains an array. I'm wondering how to correctly bind to my data contract. If anyone can give me a pointer here I would really appreciate it. Currently my cart object is null

This is what my service interface looks like:

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

My Client call is as follows

CLIENT CALL

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

}

My JSON looks like this

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

解决方案

The raw request for your WCF REST method should look as below from Fiddler:

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"}

The response in JSON would look as below:

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

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

这篇关于WCF - 发布JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆