Hello!
Short description of problem: My C# library works as a COM object (in
process), but when used in COM+ (out of process), I get some strange cast
error run time.
Long description of problem:
I have a C# library which holds a form with a microsoft web browser
component (AxSHDocVw.AxWebBrowser) in it. I have COM-enabled the library,
and using it as an in-process COM-object works real fine (though I have to
set the threading model from 'both' to 'apartment' in registry).
In adition I have a client which have to use my object out-of-process. I
solved this by creating a COM+ application, and set Activation to 'Server
application'. The client can now nicely load my library as an out-of-process
COM object. Then some really strange happens... :)
This code snippet of mine is invoked by the microsoft web browser component
when its browsing is finished (only the essential code is submitted):
-----8<-------8<-------8<-------8<-------8<-------8<--
private void webBrowser_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
try
{
ICustomDoc custDoc =
(MsHtmHstInterop.ICustomDoc)webBrowser.Document;
}
catch (Exception ex)
{
textBox1.Text += "\r\nwebBrowser_DocumentComplete
exception: " + ex.Message + "\r\n" + ex.StackTrace;
}
}
-----8<-------8<-------8<-------8<-------8<-------8<--
When loaded in-process as a regular COM object, this cast works just fine.
When registered in COM+ and loaded out of process, I get an invalid cast
exception, though the browser window
seems just fine, displaying the web page.
Any idea what causes this? Is there other ways to out-of-process enable a C#
COM object? I am really stuck on this one :)
Additional information:
When compiled as a library interop registered with ThreadingModel = both
(default), I receive the following exception when the library tries to
instantiate the Microsoft Web Browser (Shell.Explorer.2):
-----8<-------8<-------8<-------8<-------8<-------8<--
Invoke() failed with exception: source:'System.Windows.Forms', code:'0',
description:'Could not instantiate ActiveX control
'8856f961-340a-11d0-a96b-00c04fd705a2' because the current thread is not in
a single-threaded apartment.'
-----8<-------8<-------8<-------8<-------8<-------8<--
I solve this problem by manually setting the threading model of my library
to 'Apartment' instead of 'Both', which is default. (done with this registry
file:)
-----8<-------8<-------8<-------8<-------8<-------8<--
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\CLSID\{FBA1E1CD-4397-11D4-9D5E-0080AD00E0D3}\InprocServer32]
"ThreadingModel"="Apartment"
-----8<-------8<-------8<-------8<-------8<-------8<--
How I create the COM+ application:
- Administrative Tools/Component Services/Com+ applications
- New Application
- Empty Application
- Server Application with name Proxy
- Run as interactive user
- Under Proxy/Components, New component
- Import component, choosing tct.proxy
Thanks!
Thomas Evensen