views:

32

answers:

3

Well I think the title explains for itself.

I'm trying to export a configuration object trough the wire, but it looks quite difficult, the class is not serializable and... it's sealed, so no heritage here.

As anyone did something like this before?

Regards

A: 

You will have to pluck out the bits you really need, put them into a separate class that is serializable and has [DataContract] and [DataMember] attributes for WCF serialization, and then you need to do the reverse on the other end.

WCF only transmits data - serialized in XML format. If you can't serialize your data as is, you need to work around it yourself - there's no magic bullet to solve this another way...

To ease the pain of copying lots of properties from one class to another, I would recommend using some library like AutoMapper that can eliminate a lot of the boring, repetitive code to assign from one object to another.

marc_s
Well I was trying to avoid creating all the objects again. Even using a tool for it. First I'm gonna try to serialize the sections. And if it doesn't work. Then I'll go for that...
gjsduarte
A: 

That class is more of a pass-through to access the application's config file. Serializing it doesn't really make much sense, but what you may want to do is pull the values out of it and stuff them into a new class that you control. Then all you have to do is make sure that new class is serializable.

Jacob Ewald
Thanks, I'm gonna try that...
gjsduarte
A: 

Well I think I found a good solution for this issue. It passes by serializing the sections instead of the Configuration object itself. So, to ensure all the sections I need get Serialized/Deserialized together I've wrapped them all in one ConfigurationSectionGroup. I does the job, and allows me to export and import settings trough a WCF service, or directly on file. Here's the code I used:

A configuration section base class:

public abstract class ConfigurationSectionBase : ConfigurationSection
{
    public string Serialize()
    {
        return SerializeSection(null, Name, ConfigurationSaveMode.Minimal);
    }

    public void Deserialize(string configuration)
    {
        XmlReader reader = XmlReader.Create(new StringReader(configuration));
        if (!reader.ReadToFollowing(Name)) return;
        StringBuilder stringBuilder = new StringBuilder().Append(reader.ReadOuterXml());
        var stringReader = new StringReader(stringBuilder.ToString());
        reader = XmlReader.Create(stringReader);
        DeserializeSection(reader);
    }
}        

Hope it helps someone...

Regards

gjsduarte