0
I have a WCF service with a variable nullable Datetime in a Datacontract as shown below. Because of business rules this Datamember may not have the Emitdefaultvalue setado to true and the guy has to be "Datetime?"
[DataContract (Name = "DADOS")]
classe public Dados
{
[DataMember (EmitDefaultValue = false, Name = "NASCIMENTO")]
public DateTime? DtNascimento = null;
}
Man Datacontract is specified as below, see that I have to have two versions of the method Webinvoke to 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 Dtnascimento comes correctly populated with a good valid database value, everything works fine, when it comes with a null database value, the XML/JSON response comes without the BIRTH tag, ok, this is happening because Emitdefaultvalue = false. I can define, through procedures that my database send me an empty value, only when I do this the serialized object comes with a Mindate value in the answers.
Xml version:
<DADOS>
<NASCIMENTO> 1900-01-01T00: 00: 00 </ NASCIMENTO>
</ DADOS>
Json version:
{
"NASCIMENTO": "/ Date (-2208981600000-0200) /",
}
What I really need is an empty variable(tag) being shown in the answers when this value is null, this is because there are other connected systems 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": "",
}
Could someone help me with some suggestion?
what happens when you put the
IsRequired = true
next to theEmitDefaultValue = false
and sends a null? I believe that’s what you’re looking for.– Marciano.Andrade
@Andrade, I started receiving a System.ServiceModel.Communicationexception. I think it’s because I’m getting zero comic book value and marking him as Required in my contract. When BD responds with a non-zero value, Exception does not occur.
– LeoFraietta