Die! Die! Excel, you Fiend !

I was recently drafted to write an ASP Site that documents on the server to create a "status" page. Given the affinity towards Word and Excel for people in QA, it was no surprise I was soon wrestling with Excel to read some stuff from an xls file.

I got the site running with some major hiccups like not finding type definitions for objects returned by some functions. In the end I ended up using Reflection to get at the custom properties in the excel document.

Now to my horror, I found that the task manager showing zillions of Excel instances quietly ganging up on my web server. So I asked around and I was amazed at the reputation Excel had carved up for itself. I first suspected COM References... I have a negative bias towards unmanaged code of any nature.
First stop: Guidelines on how to release Word or Excel if it is not shutting down gracefully
http://support.microsoft.com/kb/317109/

Still no luck! Next I was lucky to have my logic in a separate DLL that I was test driving via NUnit. This allowed me to make the interesting observation that a test run did not leave Excel instances running. That means my code is good. It has something to do with that ASP.NET 2.0 web page. I did a quick scan and found I had hardly any code in there to cause this. More googling and finally it dawned on me

I will post the gems I found after a week of manic activity

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when run in this environment.

WARNING: Office was not designed, and is not safe, for unattended execution on a server. Developers who use Office in this manner do so at their own risk.

http://support.microsoft.com/kb/257757/
http://support.microsoft.com/kb/288368/

The answer seems to be that Office applications were not built for scalabilty or running in a multi-threaded environment. So until Office 2007 hold on to your horses !!!

I ended up not creating multiple instances and settling for an instance that is stashed away as a static variable, which seems to slowly eating up memory. But this is an internal web site, so I don't mind.... I think ASP.NET will restart the app if the memory consumed goes above a certain limit.