发送JSON日期到WCF服务 [英] Send JSON date to WCF service

查看:115
本文介绍了发送JSON日期到WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我唯一的问题是他的date属性。我得到一个jquery datepicker的日期,我想要得到它在我的服务作为c#datetime。



我的服务:

 命名空间员工
{
[ServiceContract]
公共接口IService1
{
[OperationContract]
[WebInvoke(Method =POST,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
bool UpdateEmployee(Employee Employee) ;
}
}

这是Employee:

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

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

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

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

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

解决方案

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 ,您可以在下面的代码中查看。

 命名空间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服务: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;组; }

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

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

public DateTime Hired {get;组; }

[DataMember(Name =Hired)]
private string HiredForSerialization {get;组;

[OnSerializing]
void OnSerializing(StreamingContext ctx)
{
this.HiredForSerialization = this.Hired.ToString(yyyy-MM-dd,CultureInfo不变文化);
}

[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调用: / p>

  function StackOverflow_11105856_Test(){
var url =/StackOverflow_11105856.svc/UpdateEmployee;
var data = {
名称:John Doe,
部门:会计,
工资:50000,
雇用:$(#StackOverflow_11105856_datepicker) .val()
};
$ .ajax({
type:'POST',
url:url,
contentType:application / json,
data:JSON.stringify({ Employee:data}),
success:function(result){
$(#result)。text(result.UpdateEmployeeResult);
}
});
}


I want to post json object to my WCF service

My only problem is his date property. I get the date from an jquery datepicker and i want to get it in my service as c# datetime.

My service:

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

And this is 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; }
}

All the other properties work fine. I just need to convert my date string to json date.

解决方案

The expected format for DateTime object is not the format returned by jQuery's date picker. WCF expects the date in the ASP.NET format (e.g., \/Date(1234567890)\/).

You can use other formats, though, but it's not simple (at least not until .NET 4.0; on 4.5 this got a lot better). Basically, you'd use a string property (which can be private, if your service is running under full trust) which would get the value from the wire, then hook it up to a DateTime property during the serialization episodes. There's more information about this trick at http://blogs.msdn.com/b/carlosfigueira/archive/2011/09/06/wcf-extensibility-serialization-callbacks.aspx, and you can see it on the code below.

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);
        }
    }
}

And the jQuery call:

    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);
            }
        });
    }

这篇关于发送JSON日期到WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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