【问题标题】:WCF Rest Api Datetime JSON Serialization IssueWCF Rest Api 日期时间 JSON 序列化问题
【发布时间】:2017-05-04 16:22:05
【问题描述】:

当我将 Javascript 日期类型作为参数发送到 WCF 方法时,会出现以下序列化问题。 (他们都在使用 JSON)。

DateTime content '2017-05-04T13:09:11.737Z' does not start with '\/Date(' and end with ')\/' as required for JSON.'. 

解决此序列化问题的最佳方法是什么?似乎它可以通过字符串操作来解决,但是对于包含DateTime 类型的所有 WCF 方法,是否有任何通用方法? (客户端或服务器端)

这是测试 Angular Http 帖子

this.http.post(myTestDateUrl, JSON.stringify( {date : new Date()} ) , {headers: this.getHeaders()})
     .subscribe(
        data => {
          console.log(data);
        },
        error => {
          console.log(error);
        }
      );

private getHeaders() {

    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return headers;
  }

这是 WCF 测试方法

[WebInvoke(
Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
public Stream TestDate(DateTime date)
{
    try
    {
        dynamic result = new { status = "OK"};
        return Send(result);
    }
    catch (Exception ex)
    {
        dynamic result = new { status = "ERROR", error = ex.Message };
        return Send(result);
    }
}

【问题讨论】:

标签: javascript json angular wcf typescript


【解决方案1】:

我建议根据this使用ISO 8601格式:

There is no right format; The JSON specification does not specify a format for exchanging dates which is why there are so many different

方法。

The best format is arguably a date represented in 8601 format; it is a well known and widely used format and can be handled across many

不同的语言,使其非常适合互操作性。 例如,如果您可以控制生成的 json,则提供 数据以json格式传到其他系统,日期选择8601 交换格式是一个不错的选择。

所以你可以创建这样的东西:

    public Stream TestDate(string date)
    {
        try
        {
            DateTime d;
            DateTime.TryParseExact(date, @"yyyy-MM-dd\THH:mm:ss\Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out d);
            dynamic result = new { status = "OK" };
            return Send(result);
        }
        catch (Exception ex)
        {
            dynamic result = new { status = "ERROR", error = ex.Message };
            return Send(result);
        }
    }

【讨论】:

  • 我正在寻找更改 Global.asax 文件中的 WCF 默认日期时间序列化程序设置,但我找不到方法。实际上它应该像 Web API 一样存在。此链接为 Web API 显示如何更改 DateTime 转换器。 himanshudesai.wordpress.com/2013/01/31/…
  • 亲爱的@BlackEagle。我使用 wcf 大约 3 年。我向你保证,没有合适的方法(比如 Web API)来做这些事情。在这里您可以阅读更多相关信息:stackoverflow.com/questions/18511147/…
【解决方案2】:

我已经使用以下转换器在客户端处理了这个问题。

export class DateConverter{

    static convertToWCFFormat(date:Date):string{
        return "\/Date(" + date.getTime()+date.getTimezoneOffset() + ")\/";
    }

     static convertToWCFFormatFromStr(date:string):string{
         let _date=new Date(date);
        return "\/Date(" + _date.getTime()+_date.getTimezoneOffset() + ")\/";
    }

    static converToISOFormat(dateStr:string):Date{
        return new Date(parseInt(dateStr.substr(6)));
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-08
    • 2011-01-26
    • 2012-08-14
    • 1970-01-01
    • 2012-01-10
    • 2011-12-19
    • 2016-03-29
    • 1970-01-01
    相关资源
    最近更新 更多