简体   繁体   中英

WCF/WPF DateTime returning null

I have a WPF app that is calling a WCF service. In the WCF service I make a call to a database and return some items, one which includes a datetime.

Here is how i'm returning the data.

List<MessageData> messages = new List<MessageData>();
//sql stuff
 while (reader.Read())
{
    MessageData message = new MessageData();
    message.ID = (Guid)reader["message_id"];
    message.Description = reader["subject"].ToString();
    messageData.IssuedDate = reader["issued_date"].ToString();
    message.Severity = reader["severity"].ToString();
    message.Specialty =reader["specialty"].ToString();
    messages.Add(message);
}

in MessageData I have specified IssuedDate as both a DateTime and a string. Stepping through the debugger I see an actual DateTime being placed in IssuedDate. When it comes back to the WPF app, IssuedDate is null and the value is lost.

WPF side

List<CWeb.MessageData> data = new CWeb.SCClient().GetMessages(messageData, Global.Token).ToList();

Any hints? I'm guessing it's being lost in serialization/deserialization?

Update When I assign random words to IssuedDate when it's a string, example: 'why you no work' It comes back as that string not refused to return anything in a datetime format.

Could it be your contracts don't match? Maybe the client expects a DateTime where the service provides a String ?

WCF is well capable of returning a DateTime in a data contract. So MessageData.IssuedDate should be DateTime and you could use something like this to get it from the data reader:

message.IssuedDate = reader["issued_date"] as DateTime? : DateTime.MinValue;

One more thing I noticed: The IssuedDate property is the only one you don't set in the message object, but the messageData object. Are you sure you're using the correct object?

Also, when returning DateTime as string, you should convert it into a somewhat international format you can convert back from reliably, for example ToString("yyyyMMdd HH:mm:ss") .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM