If you create your STA COM object on a .NET STA Thread, all calls to your object are marshalled to that thread.
If you create your STA COM object on a .NET MTA Thread, the runtime will create a STA thread and marshall all calls to that thread.
So, when your (STA) thread exists, your COM objects are inaccessable.
A solution might be to create the objects on a new thread for which you can control the lifetime.
I have done a similar thing like that:
using (ManualResetEventSlim mre = new ManualResetEventSlim(false))
{
Thread _STAThread = new Thread(new ThreadStart(() =>
{
globalComObject = new ComClass();
mre.Set();
try
{
Thread.CurrentThread.Join();
}
catch (ThreadAbortException)
{
}
}));
_STAThread.SetApartmentState(ApartmentState.STA);
_STAThread.IsBackground = true;
_STAThread.Start();
mre.Wait();
}
The code starts a new thread, set the appartment to STA and waits for the creation of a COM object on that thread.
The thread itself is running till your application exits (IsBackground = true) or you kill the thread explicitly with Thread.Abort().
But keep in mind, that all calls to your COM objects are marshalled and thus executed serialized one after another on that one thread. That might be a big bottleneck in your App.
ASPCompat=true signals the ASP.NET runtime, that you are using STA COM objects and thus running the page within an STA thread. otherwise you migth get an exception or all your COM objects will run in the automatically generated STA thread shared by all requests to your page (see MSDN here: http://msdn.microsoft.com/en-us/library/zwk9h2kb(VS.80).aspx)