Article ID: 238274 - Last Review: July 15, 2004 - Revision: 3.4

How To Obtain ObjectContext with ObjectControl Inside VB COM DLL From ASP and MTS

This article was previously published under Q238274
Expand all | Collapse all

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:
    'Microsoft Transaction Server Type Library' (MTXAS.DLL)
    'Microsoft Active Server Pages Object Library' (ASP.DLL)
  3. Name the Project ObjectCtxtProject and name the Class ObjectCtxtClass.
  4. Set the Class Property MTSTransactionMode = 1 - NoTransactions.
  5. 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
    						
  6. 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
    						
  7. Compile the DLL.
  8. Add the DLL to a MTS Server/Library Package.
  9. 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
    %>
    						
  10. 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 How To Create an Empty MTS Package to Add Components for ASP (http://support.microsoft.com/kb/223406/)

Developing a Visual Basic Component for IIS/MTS (http://msdn.microsoft.com/en-us/library/ms809958.aspx)

Q230149 How To Access Session and Application Variables from Within a Visual Basic Component (http://support.microsoft.com/kb/230149/)



APPLIES TO
  • Microsoft Active Server Pages 4.0
  • Microsoft Transaction Services 2.0
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
Keywords: 
kbaspobj kbcodesnippet kbhowto KB238274
 

Article Translations

 

Related Support Centers