Walkthrough of Code to Avoid ObjectDisposedException

When you build an ASP.NET web site that contains Open XML functionality, it is possible to have two users of the web site who use that Open XML functionality at the same time.  If, for whatever reason, the usage of memory in System.IO.Packaging (therefore the Open XML SDK) goes above 10MB, then System.IO.Storage uses the classes in the System.IO.IsolatedStorage.  However, System.IO.IsolatedStorage is not thread-safe.  It is not even safe to access it from to executing programs – if the programs have the same strong name.  If application A uses IsolatedStorage, and then application B uses IsolatedStorage, and then Application A quits and cleans up IsolatedStorage, application B will fail with either an ObjectDisposedException or a NullReferenceException that is thrown from deep inside System.IO.Packaging.

In a previous post and screen-cast, Handling ObjectDisposedException in the Open XML SDK I introduce an approach whereby we can avoid these types of exceptions.  At a high level, you create and run a “daemon” that listens for messages (sent through MSMQ) requesting a document to be created.  The code is attached to that post.

The screen-cast in this post walks through the code, explaining in detail the messages that are sent back and forth from your web server to this “daemon”.

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.