Microsoft KB Archive/238274

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

Article ID: 238274

Article Last Modified on 7/15/2004

-

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 6.0 Enterprise Edition
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q238274



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
 Create a new ActiveX DLL Project in Visual Basic. 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)

 Name the Project ObjectCtxtProject and name the Class ObjectCtxtClass. Set the Class Property MTSTransactionMode = 1 - NoTransactions.  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   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  Compile the DLL.</li> Add the DLL to a MTS Server/Library Package.</li>  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 %>                       </li> 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!

</li></ol>

<div class="references_section">