Welcome to CrankyGoblin.Com Sign in | Join | Help

Public Class GeoffAppleby

Inherits Microsoft.VisualBasic.MVP : Implements IBrainFart
Jumping on the XML Serialization Band Wagon

Recently both David Hayden and Brendan Tompkins discussed some XML Serialization stuff.

David talked a bit about what XML Serialization is and gave some examples. Brendan then showed a helper class that he'd written a while ago.

Rather than leave comments on both of their posts, I thought I'd stand bold and proud and hold my head up high, and make a fool of myself in public by responding with a post of my own.

XML Serialization is something that's caused me many hours of grief, for many different reasons, but mostly it's an extremely cool way of persisting and restoring data. Most of my dealings with it have been in web services - the web services I write here at work need to pass complex types, and so it's a lot easier to do the de/serialization yourself.

I've talked a bit about it in past on my blog, but I thought I'd give a quick summary of things I've dealt with and give some links into my old posts, especially since you all want to read my blog all day, right?

When you try to serialize any collection classes (Collection, Hashtable, etc - and also any that you write that inherit from CollectionBase or implement things like IEnumerable) you're in for some very interesting times. I did a post on how I got around it here.

I then went on to complain that it was still very annoying that I had to do all the serialization myself just to get the the objects through a web service, but I found the answer I was looking for in the IXMLSerialible interface (and I did get confirmation that if you need it before .net 2.0, it's ok to use it, jsut be careful :)

The biggest issue I've come across with the XMLSerializer is the performance. I wrote about how invoking it manually in a web service caused major scalability problems, and what I did to solve it.

Finally, I wrote this post talking a little about reflection. Down the bottom of the page I mentioned that I had discovered a tool called sgen.exe which ships with VS2005, which pregenerates all the custom code normally generated during the constructor call to the XMLSerializaer.

So, that's a summary of what I've mentioned so far about serialization - since I've moved to CodeBetter and we don't have search for individual blogs anymore, I thought some people who are interested in searialization (and haven't already read them) might find them of use.

Brendan, David, my whole point of writing this was to remind you both to be careful when you use the serializer a lot, because the constructor will max out your CPU :)

Listening to: rip it up - 28 days - (3:28)
Posted: Wednesday, March 02, 2005 10:07 AM by Geoff Appleby
Filed under: , ,

Comments

Kirk Marple said:

Hi Geoff,

I'm walking down the same roads you are... WSE 2.0 stuff with heavy XML serialization.

I have some utility methods, similar to Brendan's helpers, for going between XmlDocuments and CLR objects.

Using an XmlSerializer cache is a great idea, and i'm just wondering if you've compared using sgen to your XmlSerializer cache. Since both ways are essentially precompiled - at runtime vs. build time, i'd imagine the perf is the same after the precompile step?

Thx for reminding me about caching the XmlSerializers. :) I'm caching things all over the place, but had forgotten to implement that in my Whidbey project.

Kirk
# March 2, 2005 7:00 AM

Brendan Tompkins said:

Thanks, Geoff. I'm doing lots of minor deserilization all the time on our public site. I'm definitely going to look into sgen.exe. Very cool, I've got a bunch of reading to do!
# March 2, 2005 7:43 AM

Brendan Tompkins said:

Geoff,

Did you hit on the versioning problems with Serialization? That's got to be the biggest ass biters for me so far in dealing with it.
# March 2, 2005 7:48 AM

Geoff Appleby said:

I very rarely serialize to a file, it's all been for webservices. versioning has never come up :)
# March 2, 2005 8:49 AM

Geoff Appleby said:

Kirk: sgen is only in VS2005, so i've only played, not used in anger. The caching still gives some load, but not nearly as much as reconstructing all teh time.
sgen is the much better solution is that there's no load on the machine in the constructor any more - it'll be much faster overall and much more scalable - there'll be pretty much no need for a cache after it's available for public consumption.

However, the way i implemented the cache is still nice for scalability. by locking cache items while they're in use, even if the website gets hit by 50 concurrent users, only 5 (say) can serialize at any one time, so the load on the machine is never very high :)
# March 2, 2005 8:52 AM

Kirk Marple said:

i just noticed in the project properties (in Whidbey Nov CTP) there's a checkbox for 'generate serialization assembly'.

so, it looks like in Beta 2 they might call sgen during the build for us. when Beta 2 comes out, i'll have to revisit that...

for now, the XmlSerializer cache is working nicely... i didn't lock the serializers while in use, but i did lock the static Dictionary<> cache object lookup/add... i haven't noticed any concurrency issues yet under load.
# March 2, 2005 8:58 AM

Geoff Appleby said:

Fantastic! I was really hoping that I wouldn't need to fall down to the command line :)
# March 2, 2005 10:10 AM

JosephCooney said:

Geoff - I think the perf issues you were probably seeing with the Xml Serializer arise from the fact that you're not using the default constructor, and a new reader/writer pair is generated each time (these dynamically generated and loaded reader/writer pairs can't be unloaded either, so this is a good way to leak memory in your .NET app). I gave a more detailed explaination here: http://blogs.crankygoblin.com/blogs/geoff.appleby/archive/2005/01/14/44135.aspx

Brendan - I'm really surprised you had versioning problems with the Xml Serializer - it is the most version friendly serializer there is. The "CLR" serializers (binary or soap) are totally version-intolerant in framework 1.x.

# March 7, 2005 6:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

To submit your comment, click on these pictures:
  • Happy Geoff
  • Geoff's mother (normal)
  • Geoff the big mouth
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Gaptcha Image - No Peeking! Gaptcha Image - No Peeking! Gaptcha Image - No Peeking!
Can't recognise the people in these pictures? Look here for a quick introduction.
There's a time limit for you to get your comment submitted before this set of pictures expires. If you think it's been longer than 10 minutes, get some new pictures first (you won't lose what you've typed so far).
Get some new pictures 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS