I have a WCF service with a nullable DateTime variable in a DataContract as shown below. Because of business rules this DataMember can not have the EmitDefaultValue set to true and the type must be DateTime?
[DataContract (Name = "DADOS")]
classe public Dados
{
[DataMember (EmitDefaultValue = false, Name = "NASCIMENTO")]
public DateTime? DtNascimento = null;
}
My DataContract is specified as below, see that I have to have two versions of the WebInvoke maintain interoperability between different systems (responses in JSON and XML format):
[ServiceContract]
public interface IRestService
{
[OperationContract (Name = "ConsultaDadosXml")]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "ConsultaDados/xml?token={token}")]
Dados ConsultaDadosXml (token string);
[OperationContract (Name = "ConsultaDadosJson")]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "ConsultaDados/json?token={token}")]
Dados ConsultaDadosJson (token string);
}
The problem is that when DtNavigation comes correctly filled with a good valid database value, everything works fine, when it comes with a null value of the database, the XML / JSON response comes without the BIRTH tag, okay, this is happening because EmitDefaultValue = false . I can set, via procedures my database to send me an empty value, but when I do this the serialized object comes with a MinDate value in the responses.
xml version:
<DADOS>
<NASCIMENTO> 1900-01-01T00: 00: 00 </ NASCIMENTO>
</ DADOS>
Json version:
{
"NASCIMENTO": "/ Date (-2208981600000-0200) /",
}
What I really need is an empty tag being shown in the responses when this value is null, because there are other systems connected in the web service trying to interpret these values, so the best solution would be to keep these variables empty as follows:
Xml:
<DADOS>
<NASCIMENTO> </ NASCIMENTO>
</ DADOS>
Json:
{
"NASCIMENTO": "",
}
Can anyone help me with any suggestions?