How do I expose a TimeSpan through a WCF Data Service?

三世轮回 提交于 2019-12-01 04:58:35

问题


I am creating a WCF Data Service for my database of appointments.

I'm storing the appointment as a DateTime with a duration of type TimeSpan. When I attempt to access my data service, I get the following error:

"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."

Any idea how I can represent a time duration and have it accessible through my WCF Data Service?


回答1:


I would suggest exposing a new property for serialization (marked with the DataMemberAttribute) that use the Ticks property of your original timespan.

For example:

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

I'm not sure what the accessor requirements for serialization will be. Maybe you could use protected instead of public.




回答2:


You could expose the duration as Ticks, TotalSeconds, or some other primitive that can be calc'ed to hours, minutes, etc?



来源:https://stackoverflow.com/questions/5587433/how-do-i-expose-a-timespan-through-a-wcf-data-service

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