Supporting Ultra-High Performance Open XML Document Generation

The Open XML SDK is not thread-safe.  If you attempt to gain more Open XML document generation performance by using a straight-forward multi-threaded approach, you will quite possibly eventually see ObjectDisposedExceptions thrown from deep inside System.IO.Packaging.  Further, it won’t be very predictable.  Sometimes your program may not exceed a specific threshold of memory usage, and all will work as expected.  Another time, you may exceed this threshold (approximately 10MB) and you will see these exceptions, and it will not be clear why.

The following screen-cast demonstrates an approach to completely avoid these types of spurious errors.  Instead of using multiple threads, it uses multiple processes (each with its own strong name) to achieve more efficient use of a multi-core CPU.  The example code is attached to this post.

The explanation of the basic issues is repeated in videos #1, #3, and #4, so that you can go directly to the video that most closely addresses your scenario.

Link Summary
1 Handling ObjectDisposedExceptions in the Open XML SDK. Discusses the root cause of the spurious ObjectDisposedExceptions, and discusses one approach to mitigate them. This screen-cast focuses on the fix for the scenario where you have a web site with Open XML functionality, and it is possible that two users are accessing the Open XML SDK at the same time.
2 Walkthrough of Code that avoids ObjectDisposedExceptions Walks through the code that I introduced in the first screen-cast.
3 Ultra High-Performance Open XML Document Generation Demonstrates an approach that uses multiple processes to enable ultra-high performance Open XML document generation, while avoiding the thrown ObjectDisposedExceptions that you would see if you took a naive multi-threaded approach.
4 Ultra High-Performance Open XML Document Modification and Processing In a similar way to screen-cast #3 in this series, this screen-cast demonstrates an approach that uses multiple processes to enable ultra-high performance Open XML document generation, while avoiding the thrown ObjectDisposedExceptions that you would see if you took a naive multi-threaded approach. Processing documents (as opposed to modifying documents) complicates issues a bit. We need to be prepared for invalid documents that cause code to throw exceptions, and for documents that cause code to hang.
5 Ultra High-Performance Open XML Document Modification and Processing Code Walkthrough Walks through the code that I introduced in the fourth screen-cast.

Download – Example code