FIX: System.InvalidOperationException If You Use XslTransform in Multithreaded Scenario

Article translations Article translations
Article ID: 320011 - View products that this article applies to.
This article was previously published under Q320011
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

On This Page

SYMPTOMS

If you share an XslTransform object from the System.Xml.Xsl namespace between multiple threads, you may receive the following error message when you build your application:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
The call stack may appear as follows:
at System.Collections.ArrayListEnumeratorSimple.MoveNext()
    at System.Xml.XPath.XsltFunction.SetXsltContext(XsltContext context)
    at System.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsManager)
    at System.Xml.Xsl.Processor.GetValueQuery(Int32 key)
    at System.Xml.Xsl.Processor.ValueOf(ActionFrame context, Int32 key)
    at System.Xml.Xsl.ValueOfAction.Execute(Processor processor, ActionFrame frame)
    at System.Xml.Xsl.ActionFrame.Execute(Processor processor)
    at System.Xml.Xsl.Processor.Execute()
    at System.Xml.Xsl.XslTransform.Transform(IXPathNavigable input,XsltArgumentList args, TextWriter output)
				
This problem has only been seen to occur on computers that have more than one processor, although it is possible that this problem may also occur on a single processor computer.

CAUSE

This problem occurs because of a bug in the System.Xml.dll assembly.

RESOLUTION

To resolve this problem, obtain the latest service pack for Microsoft .NET Framework. For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
318836 INFO: How to Obtain the Latest .NET Framework Service Pack
NOTE: Apply this fix to systems that already have Microsoft .NET Framework with Service Pack 1 installed.

The English version of this fix should have the following file attributes or later:
   Date          Version        Size              File name
   -------------------------------------------------------------
   03-APR-2002   1.0.3705.223   1,294,336 bytes   System.Xml.dll	
				

WORKAROUND

To work around this problem, use the lock statement (in Microsoft Visual C# .NET) or the SyncLock...End SyncLock construct (in Microsoft Visual Basic .NET) to protect the use of the Transform method. However, this workaround may incur an unacceptable performance penalty, depending on the design and the purpose of your application.

Another workaround that may offer better performance is to create a solution that implements a pool or a cache of N+1 XslTransform objects, where N is the number of processors on the computer.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article. This problem was first corrected in Microsoft .NET Framework Service Pack 2 (SP2).

Properties

Article ID: 320011 - Last Review: October 24, 2013 - Revision: 3.1
APPLIES TO
  • Microsoft .NET Framework Service Pack 1
  • Microsoft .NET Framework Software Development Kit 1.0 Service Pack 2
Keywords: 
kbnosurvey kbarchive kbhotfixserver kbqfe kbbug kbfix kbnetframe100presp2fix kbnetframe100sp2fix KB320011

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com