向 WCF 服务发送 JSON 数据

4

我希望将JSON对象发布到我的WCF服务。

我的唯一问题是日期属性。我从jquery日期选择器获取日期,并希望在我的服务中将其作为C#日期时间获取。

我的服务:

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日期。

2个回答

5
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中遇到了错误。“日期格式不正确”。 - MARKAND Bhatt

0

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

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接