How to activate a COM+ component in its caller's context (261096)



The information in this article 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.
  1. 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+

  2. Add two classes named ParentClass and ChildClass to ContextTest.
  3. Set a reference to the ComSvcs.dll assembly.
  4. 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
    					
  5. 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("ContextTest.ChildClass")
        Str2 = o.GetMyContext
    
        GetMyContext = "The ContextID for the ParentClass is " & str1 & "." & vbcrlf & _
            "The ContextID for the ChildClass is " & str2 & "."
    
        Set objCtx = Nothing
        Set o = Nothing
    
    End Function
    					
  6. Save and compile the ContextTest project. Ensure that Binary Compatibility is set.
  7. 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.
  8. 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("ContextTest.ParentClass")
        MsgBox o.getmycontext
    
        Set o = Nothing
    
    End Sub
    					
  9. Save and compile the client project.
  10. 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 "Install (not Import) Your Components into COM+" section of the following Microsoft Web site:
  11. 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.
  12. 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:
    1. On the Transactions tab, set the Transaction Support to Disabled.
    2. On the Activation tab, clear the Enable Just In Time Activation and Component supports events and statistics check boxes.
    3. On the Concurrency tab, set Synchronization Support to Disabled.
  13. 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.

REFERENCES

For a Microsoft Microsoft Visual Basic .NET version of this article, see 315707.
For an in-depth description of contexts and the role they play in how components work, refer to the COM+ documentation on COM+ contexts and component activation.

Modification Type:MinorLast Reviewed:8/30/2004
Keywords:kbhowto KB261096