We have defined a COM object in C# on the .Net platform (v2.0.50727) in
an out-of-process server (.exe). This COM object, in one of its
methods, creates a WebBrowser control (AxSHDocVw.AxWebBrowser). The
main application can successfully create an instance of this object,
but when trying to call the method creating the WebBrowser control, an
exception is thrown stating that
System.Windows.Forms: ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because
the current thread is not in a single-threaded apartment.

The Main() function in the COM server has the [STAThread] attribute
set, and we checked that this thread actually runs in a single thread
appartment. The constructor of the COM object is also called on this
thread, in STA. The method creating the web browser control, however,
is called on another thread, which indeed runs in MTA. We did not
create this thread, nor did (willingly) specify somewhere that a thread
should be created, and so we do not have control over the apartment of
that thread.

I don't know whether it is relevant, but the class factory of the COM
object is registered with the flags
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE,
after which the Main() function goes on to accept COM requests by
calling
System.Windows.Forms.Application.Run ();

I'd be very grateful for any clues or hints.


Jeroen