Microsoft KB Archive/261096

= How to activate a COM+ component in its caller's context =

Article ID: 261096

Article Last Modified on 12/5/2007

-

APPLIES TO


 * Microsoft COM+ 1.0
 * Microsoft COM+ 1.5

-



This article was previously published under Q261096



SUMMARY
COM+ is an evolutionary extension to the Component Object Model (COM). This article describes the COM+ component property settings that are required to have a COM+ component created in the context of its creating COM+ components.



MORE INFORMATION
In most COM+ applications, COM+ components create and use each other when performing work. As a performance optimization, COM+ will, if possible, create a child component in the same context as that of its creating component. A very specific set of component and application property settings must be in place in order for this to happen. Those settings are as follows:
 * The application security property Perform access checks only at the process level must be selected for the COM+ application.
 * Just In Time (JIT) activation must be disabled for the child component.
 * The Concurrency property for the child component must be set to either Disabled or Supports. Requires, Requires New, or Not Supported settings prevent activation in the parent's context.
 * The Concurrency property for the child component must be set to either Disabled, Supports, or Requires. The Requires New or Not Supported settings prevent activation in the parent's context.
 * The child component's Transaction property must be set to Disabled.
 * The Component Supports Events and Statistics Activation property for the child component must be turned off.
 * The child component must have a threading model that is compatible with that of its parent. See the COM+ documentation on concurrency and apartments for information on which threading models are compatible with each other. In general, you can set the child component's threading model to Both or to the same value as the parent.
 * The application security setting Enable Authentication must be selected for library applications.

Another way to create a child component in the same context as its creating component is to enable the Must be activated in the callers context Activation property of the child component.

Note The creating (parent) object can have any valid combination of transaction, JIT, concurrency, and tracking properties selected.

If you enable the Must be activated in the callers context Activation property of the child component, only the child component can be activated if it can share the context of the parent. If you want to guarantee this, you can set this property, but it is rare that this would be necessary. If the child cannot share the context of the parent, the activation will fail with a 0x80004024 (CO_E_ATTEMPT_TO_CREATE_OUTSIDE_CLIENT_CONTEXT) error.

Step-by-Step Example
This section demonstrates how to create a sample in which a child object is created in the same context as that of the parent object. This example uses a Microsoft Visual Basic EXE project for the client and a Visual Basic dynamic-link library (DLL) project that contains two classes.  Use the instructions in the following Microsoft Knowledge Base article to create a simple Visual Basic DLL project named ContextTest:

281630 How to configure Visual Basic DLL project properties to run in IIS, MTS, or COM+

 Add two classes named ParentClass and ChildClass to ContextTest. Set a reference to the ComSvcs.dll assembly.  Add the following code to ChildClass: Public Function GetMyContext As String Dim objCtx As ObjectContext

Set objCtx = GetObjectContext GetMyContext = CStr(objCtx.ContextInfo.GetContextId) Set objCtx = Nothing

End Function   Add the following code to ParentClass: Public Function GetMyContext As String Dim objCtx As ObjectContext Dim str1 As String Dim str2 As String

Set objCtx = GetObjectContext Str1 = CStr(objCtx.ContextInfo.GetContextId)

Dim o As Object Set o = CreateObject(&quot;ContextTest.ChildClass&quot;) Str2 = o.GetMyContext

GetMyContext = &quot;The ContextID for the ParentClass is &quot; & str1 & &quot;.&quot; & vbcrlf & _ &quot;The ContextID for the ChildClass is &quot; & str2 & &quot;.&quot;

Set objCtx = Nothing Set o = Nothing

End Function  Save and compile the ContextTest project. Ensure that Binary Compatibility is set. In Visual Basic, create a new Standard EXE project. This project will serve as the client EXE for this test. Form1 is created by default.</li>  Place a Command button on Form1. Double-click Command1 to open the code window, and add the following code: Private Sub Command1_Click Dim o As Object

Set o = CreateObject(&quot;ContextTest.ParentClass&quot;) MsgBox o.getmycontext

Set o = Nothing

End Sub </li> Save and compile the client project.</li> Install ContextTest.dll into a new COM+ server application. For more information on how to install a DLL into a COM+ application, refer to the &quot;Install (not Import) Your Components into COM+&quot; section of the following Microsoft Web site:

&quot;COM+ Application Guidelines for Visual Basic Development&quot;

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncomser/html/complus_security.asp

</li> After ContextTest.dll is installed, right-click your new COM+ application, and then click Properties. On the Security tab, set Security Level to Perform access checks only at the process level. In addition, ensure that the Enforce access checks for this application check box is cleared.</li> At the component level, set whichever properties you prefer for the ParentClass component. For the ChildClass component, right-click the ChildClass component, and then click Properties. Set the following properties for ChildClass: <ol style="list-style-type: lower-alpha;"> On the Transactions tab, set the Transaction Support to Disabled.</li> On the Activation tab, clear the Enable Just In Time Activation and Component supports events and statistics check boxes.</li> On the Concurrency tab, set Synchronization Support to Disabled.</li></ol> </li> Run your sample client, and click the command button. Notice that the ContextIDs are the same, which indicates that both objects are created in the same context.</li></ol>

<div class="references_section">