如何在WCF DataContract中序列化可为空的DateTime? [英] How to serialize nullable DateTime in a WCF DataContract?

查看:158
本文介绍了如何在WCF DataContract中序列化可为空的DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在序列化此东西时遇到麻烦.

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; }
    }
}

在生成的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>

但是会发生运行时异常并作为XML错误返回:

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

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

是的,我看到了一个类似"问题,但它与旧版.NET中的ASMX服务有关.

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

我想知道如何在.NET 4.5 WCF服务中做到这一点吗?

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

推荐答案

似乎您首先有一个DateTime(在服务器和客户端),然后将DateTime修改为可为空的DateTime,但没有更新客户端

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.

客户端现在仍然希望使用DateTime(因此不允许为null),但是服务器正在发送null.因此是例外.

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.

这篇关于如何在WCF DataContract中序列化可为空的DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆