WCF Display Format DataMember DateTime

I have a working WCF service that used JSON as RequestFormat and ResponseFormat.

[ServiceContract]     
public interface IServiceJSON 
{ 

    [OperationContract]   
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    MyClassA echo(MyClassA oMyObject); 

} 

[DataContract] 
public class MyClassA 
{ 
    [DataMember] 
    public string message; 

    [DataMember] 
    public List<MyClassB> myList; 

    public MyClassA() 
    { 
        myList = new List<MyClassB>(); 
    } 
} 

[DataContract] 
public class MyClassB 
{ 
    [DataMember] 
    public int myInt; 

    [DataMember] 
    public double myDouble; 

    [DataMember] 
    public bool myBool; 

    [DataMember] 
    public DateTime myDateTime; 

}

The myDateTime property of MyClassB is of type DateTime. This is serialized in the following format: "myDateTime": "/ Date (1329919837509 + 0100) /"

A client with whom I need to communicate cannot work with this format. This requires a more conventional format, for example, for example: yyyy-MM-dd hh: mm: ss

Can this be added to the DataMember attribute? For instance:

[DataMember format = "yyyy-MM-dd hh:mm:ss"] 
public DateTime myDateTime;

Thanks in advance!

+5
source share
2 answers

Why not just pass it as an already formatted string?

, DataContract . , .

+2

...

[DataContract]
public class ProductExport
{
    [DataMember]
    public Guid ExportID { get; set; }

    [DataMember( EmitDefaultValue = false, Name = "updateStartDate" )]
    public string UpdateStartDateStr
    {
        get
        {
            if( this.UpdateStartDate.HasValue )
                return this.UpdateStartDate.Value.ToUniversalTime().ToString( "s", CultureInfo.InvariantCulture );
            else
                return null;
        }
        set
        {
            // should implement this...
        }
    }

    // this property is not transformed to JSon. Basically hidden
    public DateTime? UpdateStartDate { get; set; }

    [DataMember]
    public ExportStatus Status { get; set; }
}

UpdateStartDate. , nullTime , DateTime? JSon .

+5

All Articles