HOWTO: Obtain ObjectContext with ObjectControl Inside VB COM DLL From ASP and MTS

ID: Q238274


The information in this article applies to:
  • Active Server Pages
  • Microsoft Transaction Server 2.0
  • Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Internet Information Server versions 4.0, 5.0


SUMMARY

This article covers the implementation of the MTS ObjectContext through the ObjectControl inside a Visual Basic component under MTS control for ASP.
This is extremely useful if you would like to have direct access to the ASP intrinsic objects; for example, Application, Session, Response, Request, and Server.

In IIS 3.0, components access ASP built-in objects through the IScriptingContext interface. Components that need to access the built-in objects do so through page-level event methods. While this method is still supported, an alternative now exists and is recommended as an improvement over page-level event methods.

Instead, use the ObjectContext Object to access the built-in objects. ObjectContext makes IIS applications better suited for large-scale scenarios and provides support for transaction processing. It is highly recommended that you convert your existing ASP-based applications to this new approach if they are high-volume applications. This conversion is required if your applications support transaction processing.

Implement the ObjectControl interface when you want to define context-specific initialization and cleanup procedures for your MTS objects and specify whether or not the objects can be recycled.

The ObjectControl interface provides the following methods:

  • Activate: Allows an object to perform context-specific initialization whenever it's activated. This method is called by the MTS run-time environment before any other methods are called on the object.


  • CanBePooled: Allows an object to notify the MTS run-time environment of whether it can be pooled for reuse. Return True if you want instances of this component to be pooled, or False if not.


  • Deactivate: Allows an object to perform whatever cleanup is necessary before it's recycled or destroyed. This method is called by the MTS run-time environment whenever an object is deactivated.



MORE INFORMATION

  1. Create a new ActiveX DLL Project in Visual Basic.


  2. Select References from the Project menu and add the following references to the project:


  3. 'Microsoft Transaction Server Type Library' (MTXAS.DLL)
    'Microsoft Active Server Pages Object Library' (ASP.DLL)
  4. Name the Project ObjectCtxtProject and name the Class ObjectCtxtClass.


  5. Set the Class Property MTSTransactionMode = 1 - NoTransactions.


  6. Copy and paste the following code into the Class Module:
    
    Implements ObjectControl
    Private objContext As ObjectContext
    Option Explicit
    Private Sub ObjectControl_Activate()
        ' Get a reference to the object's context here,
        ' so it can be used by any method that may be
        ' called during this activation of the object.
        Set objContext = GetObjectContext()
    End Sub
    Private Function ObjectControl_CanBePooled() As Boolean
        ' This object should not be recycled,
        ' so return false.
        ObjectControl_CanBePooled = False
    End Function
    Private Sub ObjectControl_Deactivate()
        ' Perform any necessary cleanup here.
        Set objContext = Nothing
    End Sub 


  7. Copy and paste the following public method into the Class Module:
    
    Public Sub TestMethodObjectCtxt()
        Dim objResponse As Response
        Dim objRequest  As Request
        
        Set objResponse = objContext("Response") ' Obtain ASP Response object
        Set objRequest  = objContext("Request")  ' Obtain ASP Request  object
        
        If InStr(objRequest.ServerVariables("HTTP_USER_AGENT"), "MSIE") > 0 Then
            objResponse.Write "You are using a very powerful browser."
        Else
            objResponse.Write "Try Internet Explorer today!"
        End If
    End Sub 


  8. Compile the DLL.


  9. Add the DLL to a MTS Server/Library Package.


  10. Copy and paste the following ASP script into a new ASP file in a virtual directory.
    
    <%
      set obj = Server.CreateObject("ObjectCtxtProject.ObjectCtxtClass")
      obj.TestMethodObjectCtxt
      set obj = Nothing
    %> 


  11. Request the ASP file from a browser, you will see the following result if you are running Internet Explorer:
    You are using a very powerful browser.
    Otherwise you will see the following result:
    Try Internet Explorer today!



REFERENCES

For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

Q223406 HOWTO: Create an Empty MTS Package to Add Components for ASP

Developing a Visual Basic Component for IIS/MTS

Additional query words: kbCOMt kbMTS kbASP

Keywords : kbASP kbCOMt kbMTS kbiis400 kbiis500
Version : WINDOWS:5.0,6.0; winnt:2.0,4.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: December 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.