How to serialize nullable DateTime in a WCF DataContract?

大憨熊 提交于 2019-12-24 12:42:06

问题


I am having trouble serializing this thing.

namespace Such.Namespace.Wow
{
    [DataContract(Namespace = "http://very.namespace.uh")]
    public class DogeResponse
    {
        [DataMember]
        public virtual int Foo { get; set; }

        [DataMember]
        public virtual DateTime? ExpiringDate { get; set; }
    }
}

In generated WSDL :

<xs:complexType name="DogeResponse">  
<xs:sequence>
<xs:element minOccurs="0" name="Foo" type="xs:int"/>  
<xs:element minOccurs="0" name="ExpiringDate" nillable="true" type="xs:dateTime"/>  
</xs:sequence></xs:complexType>

But the run-time exception occurs and returns as XML fault:

<SerializationException>
<Message>ValueType 'System.DateTime' cannot be null.</Message>

Yes I saw a "similar" question but it was about ASMX service in somewhat older .NET.

I wonder how to do it in .NET 4.5 WCF service?


回答1:


It seems like you first had a DateTime (on server and client side) and than you modified the DateTime to a nullable DateTime, but did not update the client side.

The client side now still expects a DateTime (so no null allowed) but the server is sending a null. Hence the exception.

Please update the client side service reference.




回答2:


Be carefull with parsing XML. element is not a valid null. This is considered an empty string and so not convertable to a datetime. For it to be null it needs to have that special nill=true attribute.

A clarification: If you have a book xml element and one of the child elements is book:series you have several options when filling it out:

Removing the element entirely - This can be done when you wish to indicate that series does not apply to this book or that book is not part of a series. In this case xsl transforms (or other event based processors) that have a template that matches book:series will never be called. For example, if your xsl turns the book element into table row (xhtml:tr) you may get the incorrect number of table cells (xhtml:td) using this method. Leaving the element empty - This could indicate that the series is "", or is unknown, or that the book is not part of a series. Any xsl transform (or other evernt based parser) that matches book:series will be called. The value of current() will be "". You will get the same number of xhtml:td tags using this method as with the next described one. Using xsi:nil="true" - This signifies that the book:series element is NULL, not just empty. Your xsl transform (or other event based parser) that have a template matching book:series will be called. The value of current() will be empty (not empty string). The main difference between this method and (2) is that the schema type of book:series does not need to allow the empty string ("") as a valid value. This makes no real sense for a series element, but for a language element that is defined as an enumerated type in the schema, xsi:nil="true" allows the element to have no data. Another example would be elements of type decimal. If you want them to be empty you can union an enumerated string that only allows "" and a decimal, or use a decimal that is nillable.



来源:https://stackoverflow.com/questions/32558502/how-to-serialize-nullable-datetime-in-a-wcf-datacontract

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!