如何通过WCF数据服务公开TimeSpan?

问题描述:

我正在为约会数据库创建一个WCF数据服务。如何通过WCF数据服务公开TimeSpan?

我将约会存储为类型为TimeSpan的持续时间的DateTime。当我试图访问我的数据服务,我得到以下错误:

"The server encountered an error processing the request. The exception message is 'The property 'Duration' on type 'Appointment' is of type 'Time' which is not a supported primitive type.'. See server logs for more details."

任何想法,我怎么能代表一个时间段,并把它通过访问我的WCF数据服务?

我会建议公开一个使用原始时间段的Ticks属性的序列化新属性(标记为DataMemberAttribute)。

例如:

[DataMember("TheTimeSpanTicks")] 
public long TheTimeSpanTicks 
{ 
    get { return TheTimeSpan.Ticks; } 
    set { TheTimeSpan = new TimeSpan(value); } 
} 

我不知道序列化的存取要求是什么。也许你可以使用protected而不是public

您可以将持续时间暴露为Ticks,TotalSeconds或其他可以计算为小时,分钟等的基元?