Skip to main content

Problem:

Previously the customer was asking about using REENTRANT(2) to achieve a multi-user COM component for use with Microsoft.NET. They have now developed that module and it is working well.

Whilst working on that project they found that all COM modules within an application seem to run sequentially. Let me give an example to explain what I mean by this:

This is an ASP.NET Web application.

User 1 runs program A.aspx which calls COM module AA.dll.

If User 2 runs program B.aspx and this tries to call COM module BB.dll then this call will be suspended until the Method in AA.dll completes.

They can see that if we want 2 users to be able to run the same program and call the same COM module simultaneously then this will need to be built as REENTRANT(2).

They are not sure what we have to do to resolve the problem that only one COM module will execute at any one time and so all calls to Methods in different COM modules are effectively queued up and executed sequentially.

Resolution:

In order for an ASP.NET page to call a COM server correctly you must specify the Page Attribute: "AspCompat=True" in the header of the .aspx file from which the COM Server will be instantiated and invoked.

This is because COM requires that the page be executed on a single-threaded apartment (STA) thread.

The AspCompat attribute makes the page behave as if it were running as a ASP (non .NET) application.

This issue is down to how COM threading models work. Its an architectural issue imposed by Microsoft. You would get the same behaviour if you tried to call Visual Basic 6 COM DLLs (or C COM Objects marked as Apartment Threaded).

If they are COBOL COM DLLs its very likely they are "Apartment" threaded. This means the COM Object will always be called on the same thread that created the COM Object. This is good from the COBOL point of view as you can use REENTRANT"2" to get your working storage tied to that  thread.

However in .Net by default there is only 1 Single Threaded Apartment (STA) thread. That means that calls to the COM objects are Queued to be executed on the only STA thread. Therefore no matter how many ASP.Net request are concurrently executing any calls to COM objects go to the 1 STA Thread.

The suggestion above regarding the usage of "AspCompat=True" to turn on compatability mode means that ASP.Net will create and use a STA Thread pool hence the requests to the COM Objects are executed in its own apartment and are not passed to another STA Thread.

Old KB# 4291