This post originated from an RSS feed registered with .NET Buzz
by Scott Hanselman.
Original Post: Are XmlSerializers ThreadSafe?
Feed Title: Scott Hanselman's ComputerZen.com
Feed URL: http://radio-weblogs.com/0106747/rss.xml
Feed Description: Scott Hanselman's ComputerZen.com is a .NET/WebServices/XML Weblog. I offer details of obscurities (internals of ASP.NET, WebServices, XML, etc) and best practices from real world scenarios.
It's expensive to make XmlSerializers (until .NET 2.0 when sgen.exe comes out and
I can pre-created and compile Serializers.
I made an XmlSerializer Factory:
public
class XmlSerializerFactory
{ private XmlSerializerFactory(){} privatestatic Hashtable
serializers = new Hashtable(); publicstatic XmlSerializer
GetSerializer(Type t)
{
XmlSerializer xs = null; lock(serializers.SyncRoot)
{
xs = serializers[t] as XmlSerializer;
if(xs == null)
{
xs = new XmlSerializer(t);
serializers.Add(t,xs);
}
} return xs;
}
}>
This Factory is thread safe (right?) BUT Are XmlSerializer instances ThreadSafe?
The MSDN Documentation gives the standard cop-out answer "They aren't explicitly
Thread Safe..." (which means they weren't written to be, but also weren't written
NOT to be)
So, to make sure I cover the case when someone is deserializing the same type of object
using the SAME instance of a deserializer on multiple threads, I've been doing this: