The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Are XmlSerializers ThreadSafe?

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Scott Hanselman

Posts: 1031
Nickname: glucopilot
Registered: Aug, 2003

Scott Hanselman is the Chief Architect at Corillian Corporation and the Microsoft RD for Oregon.
Are XmlSerializers ThreadSafe? Posted: Dec 4, 2003 5:39 PM
Reply to this message Reply

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.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Scott Hanselman
Latest Posts From Scott Hanselman's ComputerZen.com

Advertisement

Here's the deal:

  1. It's expensive to make XmlSerializers (until .NET 2.0 when sgen.exe comes out and I can pre-created and compile Serializers.
  2. I made an XmlSerializer Factory:

    public class XmlSerializerFactory
    {
       private XmlSerializerFactory(){}
       private static Hashtable serializers = new Hashtable();
       public static 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;
        }
    }
    >

  3. 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:

XmlSerializer xs2 = XmlSerializerFactory.GetSerializer( >typeof(MyNamedType));
lock(xs2)
{
   mynameInstance = (MyNamedType)xs2.Deserialize(someStringReader);
}
>

Is the lock{} needed? Am I being paranoid? Mr. Purdy?  Bueller?

Read: Are XmlSerializers ThreadSafe?

Topic: The Mad Scientist's Club Previous Topic   Next Topic Topic: Floating Point Arithmetic

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use