Note that there are some explanatory texts on larger screens.

plurals
  1. POControlling the Threading Model of COM objects served by .NET Out of Process Servers
    primarykey
    data
    text
    <p>When creating an out-of-process COM server in C# as documented in Microsoft's All-In-One code sample: <a href="http://1code.codeplex.com/SourceControl/changeset/view/55574#522318" rel="nofollow">CSExeCOMServer</a>, it seems difficult to control the threading model of objects that are created in the server (by a client).</p> <p>The object being created needs to be in an STA due to the fact that it uses WPF objects, and it's factory is being registered as demonstrated on line 95 of <a href="http://1code.codeplex.com/SourceControl/changeset/view/55574#522314" rel="nofollow">ExeCOMServer.cs</a> and pasted below...</p> <pre><code>private void PreMessageLoop() { // // Register the COM class factories. // Guid clsidSimpleObj = new Guid(SimpleObject.ClassId); // Register the SimpleObject class object int hResult = COMNative.CoRegisterClassObject( ref clsidSimpleObj, // CLSID to be registered new SimpleObjectClassFactory(), // Class factory CLSCTX.LOCAL_SERVER, // Context to run REGCLS.MULTIPLEUSE | REGCLS.SUSPENDED, out _cookieSimpleObj); if (hResult != 0) { throw new ApplicationException( "CoRegisterClassObject failed w/err 0x" + hResult.ToString("X")); } </code></pre> <p>However, the CreateInstance function is Always called in a new thread which is in an MTA. It doesn't seem to matter that the main thread of the local server is marked (and verified) as an STA thread.</p> <p>All material on the matter that has been found intimates that the apartment of the created objects should match the apartment of the thread in which the factory was registered. In fact, this seems to be the case when using an ATL COM server (mixed with Managed C++ to create the objects), but this method appears to be injecting a new thread in to the work flow who's intialization parameters, particularly the COM threading model, do not appear to be changeable.</p> <p>Has anyone solved this issue without resorting to a COM server written largely in unmanaged code.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload