Handling ObjectDisposedException in the Open XML SDK

When building web sites that include Open XML functionality, or when building very high performance multi-threaded Open XML applications, you may encounter an ObjectDisposedException (or NullReferenceException) that is thrown from deep inside System.IO.Packaging.  The following screen-cast explains why we encounter this exception, and provides example code and guidance for mitigating it.

The example code that I demonstrate in the video is attached to this blog 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