【问题标题】:Send JSON date to WCF service将 JSON 日期发送到 WCF 服务
【发布时间】:2012-06-19 17:05:20
【问题描述】:

我想将 json 对象发布到我的 WCF 服务

我唯一的问题是他的日期属性。我从 jquery datepicker 获取日期,我想在我的服务中获取它作为 c# datetime。

我的服务:

namespace Employee
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", 
                   RequestFormat = WebMessageFormat.Json,
                   ResponseFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Wrapped)]
        bool UpdateEmployee(Employee Employee);
    }
}

这是员工:

[DataContract]
public class Employee
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Department { get; set; }

    [DataMember]
    public int Salary { get; set; }

    [DataMember]
    public DateTime Hired { get; set; }
}

所有其他属性都可以正常工作。我只需要将我的日期字符串转换为 json 日期。

【问题讨论】:

    标签: json wcf datepicker


    【解决方案1】:

    DateTime 对象的预期格式不是 jQuery 日期选择器返回的格式。 WCF 需要 ASP.NET 格式的日期(例如,\/Date(1234567890)\/)。

    不过,您可以使用其他格式,但这并不简单(至少在 .NET 4.0 之前不会;在 4.5 上这会好很多)。基本上,您将使用一个字符串属性(如果您的服务在完全信任下运行,它可以是私有的),该属性将从线路获取值,然后在序列化过程中将其连接到 DateTime 属性。 http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/06/wcf-extensibility-serialization-callbacks.aspx 有更多关于这个技巧的信息,你可以在下面的代码中看到它。

    namespace StackOverflow_11105856
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            [WebInvoke(Method = "POST",
                       RequestFormat = WebMessageFormat.Json,
                       ResponseFormat = WebMessageFormat.Json,
                       BodyStyle = WebMessageBodyStyle.Wrapped)]
            string UpdateEmployee(Employee Employee);
        }
    
        public class Service : IService1
        {
            public string UpdateEmployee(Employee Employee)
            {
                return string.Format("Name={0},Hired={1}", Employee.Name, Employee.Hired.ToString("yyyy-MM-dd HH:mm:ss"));
            }
        }
    
        [DataContract]
        public class Employee
        {
            [DataMember]
            public string Name { get; set; }
    
            [DataMember]
            public string Department { get; set; }
    
            [DataMember]
            public int Salary { get; set; }
    
            public DateTime Hired { get; set; }
    
            [DataMember(Name = "Hired")]
            private string HiredForSerialization { get; set; }
    
            [OnSerializing]
            void OnSerializing(StreamingContext ctx)
            {
                this.HiredForSerialization = this.Hired.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
            }
    
            [OnDeserializing]
            void OnDeserializing(StreamingContext ctx)
            {
                this.HiredForSerialization = "1900-01-01";
            }
    
            [OnDeserialized]
            void OnDeserialized(StreamingContext ctx)
            {
                this.Hired = DateTime.ParseExact(this.HiredForSerialization, "MM/dd/yyyy", CultureInfo.InvariantCulture);
            }
        }
    }
    

    还有 jQuery 调用:

        function StackOverflow_11105856_Test() {
            var url = "/StackOverflow_11105856.svc/UpdateEmployee";
            var data = {
                Name: "John Doe",
                Department: "Accounting",
                Salary: 50000,
                Hired: $("#StackOverflow_11105856_datepicker").val()
            };
            $.ajax({
                type: 'POST',
                url: url,
                contentType: "application/json",
                data: JSON.stringify({ Employee: data }),
                success: function (result) {
                    $("#result").text(result.UpdateEmployeeResult);
                }
            });
        }
    

    【讨论】:

    • 我什至在 OnDeserialized 中遇到错误。 “日期格式不正确”
    【解决方案2】:

    您应该尝试将属性BodyStyle = WebMessageBodyStyle.Wrapped 更改为BodyStyle = WebMessageBodyStyle.Bare。这样框架就不会添加任何额外的 XML 装饰。

    此外,您应该检查来自客户端的日期格式。 也许您应该以预设格式从客户端发送它,然后在您的对象中包含一个字符串属性,而不是 DateTime 属性。

    您可以添加一个只读属性,使用已知格式将日期字符串转换为 DateTime。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2014-11-30
      • 2011-02-24
      • 1970-01-01
      相关资源
      最近更新 更多