gpt4 book ai didi

wcf - RestSharp 到 WCF 作为 JSON 日期格式化/序列化

转载 作者:行者123 更新时间:2023-12-05 08:00:33 24 4
gpt4 key购买 nike

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

public bool SyncTimestamps(IList<Timestamp> timestamps)

在我的生活中,我无法让客户端以其对使用 RestSharp 满意的格式将值传递给主机。问题似乎与日期格式有关。

尝试 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 content '2013-08-05T13:51:13.6201529Z' does not start with '/Date(' and end with ')/' as required for JSON.'

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

尝试 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 上的文档声明需要以下格式。

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 似乎要求的格式。

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

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

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

但是,我的服务仍然以“异常消息是‘Expecting state‘Element’..”拒绝了这一点

最佳答案

除了 JSON.Net 之外,还有另一个 JSON 序列化库可以为您执行此操作:https://www.nuget.org/packages/ServiceStack.Text/

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

var json = JsonSerializer.SerializeToString(timestamps);
request.AddParameter("application/json", json, ParameterType.RequestBody);

关于wcf - RestSharp 到 WCF 作为 JSON 日期格式化/序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18060440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com