views:

358

answers:

1

Hi,

I have 'extended' the System.DateTime struct by adding some essential fields to it. Ideally I'd like to be able to deliver this object via a webservice to a winforms client.

I've marked the stuct type as [Serializable] and it also implments ISerializable, however if I inspect the XML being delivered by the webservice it simply contains an empty tag for the object.

Putting breakpoints all over the place has lead me to believe that when the object gets de-hydrated the ISerializable method void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) never appears to get called.

There are various reasons why I'd prefer to keep this as a struct, but will convert it to a class if necessary.

Does anyone know why GetObjectData is being ignored by the .net framework while it is preparing the data for the webservice response? The struct which I am working with contains a DateTime member and a few booleans.

please note, this is .net 2.0!

Cheers

+2  A: 

First, web-services use XmlSerializer - so you'd need IXmlSerializable for custom serialization. The standard XmlSerializer serialization only acts on public properties that have both a getter and setter.

Second, structs generally don't work very well as web-service DTO objects; in particular, XmlSerializer demands things be mutable... which structs shouldn't be.

I'd use a class, personally. If you can give more info, I might be able to say more...

For example:

[Serializable]
public class FunkyTime
{
    [XmlAttribute]
    public DateTime When { get; set; }
    [XmlAttribute]
    public bool IsStart { get; set; }
    [XmlAttribute]
    public bool IsEnd { get; set; }
}

(note you can tweak the xml layout / names in various ways)

Marc Gravell
Thanks, it was the IXmlSerializable interface which I needed to implment and it is all working correctly - still in a struct!
Ash
In my specific instance I'm not using [XmlAttribute] tags as I have a specific optimsed set of values which I pass in its de-hydrated state. I prepare this during WriteXml() and rehydrate the struct during ReadXml().
Ash