RestSharp 到 WCF 作为 JSON 日期格式/序列化 [英] RestSharp to WCF as JSON date formatting / serializing

查看:76
本文介绍了RestSharp 到 WCF 作为 JSON 日期格式/序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受时间戳"列表的 WCF 方法

I have a WCF Method that takes in a list of "Timestamps"

public bool SyncTimestamps(IList<Timestamp> timestamps)

在我的一生中,我无法让客户端以它对使用 RestSharp 满意的格式将值传递给主机.问题似乎出在日期格式上.

For the life of me I cannot get the client to pass values to the host in a format that it is happy with using RestSharp. The problem appears to be with dateformatting.

尝试 1

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(timestamps);

输出 1

"[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"2013-08-05T13:51:13.6201529Z\",\"TimestampActual\":\"2013-08-05T13:51:13.6201529Z\",\"Type\":1,\"Active\":false}]"

错误 1

DateTime 内容 '2013-08-05T13:51:13.6201529Z' 不以'/Date(' 并以 ')/' 结尾,这是 JSON 的要求.'

DateTime content '2013-08-05T13:51:13.6201529Z' does not start with '/Date(' and end with ')/' as required for JSON.'

我读到这是 RestSharp 序列化器的问题,所以我用 Json.Net 替换了它,它会产生一个略有不同的日期字符串.

I read that this was a problem with the RestSharp serializer so I replaced with Json.Net which produces a slightly different date string.

尝试 2

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(JsonConvert.SerializeObject(timestamps));    

输出 2

{application/json="[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"2013-08-05T14:54:33.9261815+01:00\",\"TimestampActual\":\"2013-08-05T14:54:33.9251814+01:00\",\"Type\":1,\"Active\":false}]"}

错误 2

The exception message is 'Expecting state 'Element'.. 
Encountered 'Text' with name '', namespace ''. '. See server logs for more details. 
The exception stack trace is: 

at ReadArrayOfTimestampFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString , CollectionDataContract ) 
at System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) at 

任何人都可以建议一种生成 WCF 服务乐于接受和反序列化的日期格式的方法吗?MSDN 上的文档指出需要以下格式.

Can anyone suggest a way to produce a dateformat that WCF service will be happy to accept and deserialize? The docs on MSDN state that the following format is required.

DateTime 值以 JSON 字符串的形式出现"/Date(700000+0500)/", 其中第一个数字(示例中为 700000提供)是 GMT 时区的毫秒数,常规(非夏令时)自 1970 年 1 月 1 日午夜以来的时间.

DateTime values appear as JSON strings in the form of "/Date(700000+0500)/", where the first number (700000 in the example provided) is the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970.

更新:

我发现作为 JSON.Net 一部分的设置允许我将日期转换为 WCF 似乎需要的格式.

I found a settings as part of JSON.Net that allows me to transfer the date into the format that WCF appears to be askign for.

var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
JsonConvert.SerializeObject(obj, settings);

这似乎完成了创建序列化字符串的工作.

This appears to have done the job in creating the serialized string.

{application/json="[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"\\/Date(1375713542908+0100)\\/\",\"TimestampActual\":\"\\/Date(1375713542908+0100)\\/\",\"Type\":1,\"Active\":false}]"}

但是,我的服务仍然拒绝此操作,并显示异常消息是‘预期状态‘元素’.."

My service however still rejects this with "The exception message is 'Expecting state 'Element'.."

推荐答案

您就快到了.您只需手动将参数添加到请求中,绕过有问题的 RestSharp 序列化器.

You were almost there. You just have to add the parameter to the request manually, bypassing the faulty RestSharp serializer.

var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
request.RequestFormat = DataFormat.Json;
var settings = new JsonSerializerSettings() { DateFormatHandling=DateFormatHandling.MicrosoftDateFormat };
var json = JsonConvert.SerializeObject(timestamps,settings);

request.AddParameter("application/json",json,null,ParameterType.RequestBody);   

这篇关于RestSharp 到 WCF 作为 JSON 日期格式/序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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