Error writing large files using OpenXML
We recently encountered a problem writing large files using OpenXML from an Office add-in. When run as an execuable the same code doesn't fail. The relevant part of the error message is "Unable to determine the identity of domain" which is from an IsolatedStorageException even though, at the time of the error, the code in question was writing to a MemoryStream. So what's going on?
Under the skin, OpenXML uses the System.IO.Packaging classes to read and write Office documents. After some investigation we discovered that when any part exceeds 8MB the Packaging API begins to use IsolatedStorage using a user/domain store as a scratch pad area. That's great when code is running as an executable. However when running as an add-in the managed code is started by COM infrastructure. Unfortunately, when COM creates the AppDomain instance it doesn't provide any evidence so the call to create an IsolatedStorage stream fails. The error message "Unable to determine the identity of domain" means exactly that: the AppDomain has no identity.
So what's the solution? Well if you are entirely reliant on COM to use your assembly, for example because you want to allow users to use your code in a script, then you may be out of luck. For those developing using VSTO there is, apparently, the option to install the add-in using ClickOnce which is supposed to address the issue in some way.
Our solution has been to fix the AppDomain. Once an assembly has been registered for COM interop any classes can be created directly by COM. However Microsoft used to recommend that assembly classes are instantiated indirectly using a COM shim. Fortunately for us this is our pattern so we're able to provide valid evidence when creating the AppDomain.


