ASP Intrinsics Under COM Plus in the VB Windows 2000 Component

By Peter A. Bromberg, Ph.D.

As more and more VB developers move to the Windows 2000 platform, the ability to utilize the advanced object creation and control features of Component Services becomes an important issue.

Fortunately, it is relatively easy to host a component in COM+ and gain access to the ASP Intrinsics - Server, Session, Application, Response and Request - in your component. In this article we will show how to create a component that can be hosted in a Windows 2000 COM+ Application and gain access to the ASP Intrinsics. We will also point out a couple of pitfalls that are native to Windows 2000 and how to work around them successfully.

First, under Windows 2000, fire up Visual Basic 6.0 and create a new ActiveX DLL project. Let's call our project COMPLUS. You should have a blank Class Module. Let's rename the class "TEST". Now we know that we can expect to instantiate our component in an ASP page with the ProgID "COMPLUS.Test".

The key to successful management of an ASP component in the Component Services framework is to utilize the ObjectControl interface of the COMSVCSLib Library.

We will need to set references to both the ASP Type Library and the COM+ Services Type Library:


Now here is the code to instantiate and give us access to the ASP Intrinsics using ObjectControl:

Implements ObjectControl ' we are implementing the ObjectControl interface
Private objContext As COMSVCSLib.ObjectContext ' The ObjectContext property
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
Public Sub CheckMe()
Dim Response As Response
Dim Request As Request
Dim Session As Session
Dim Application As Application
Dim Server As Server
Set Response = objContext("Response") ' Obtain ASP Response object
Set Request = objContext("Request") ' Obtain ASP Request object
Set Session = objContext("Session") ' Obtain ASP Session object
Set Application = objContext("Application") ' Obtain ASP Application object
Set Server = objContext("Server") ' Obtain ASP Server object
' You now have access to all the ASP Intrinsics in your component!
' Perform server-side browser detection ...
If InStr(Request.ServerVariables("HTTP_USER_AGENT"), "MSIE") > 0 Then
Response.Write "You are using Internet Explorer.<BR>"
Response.Write "You are not using Internet Explorer<BR>"
End If
Response.Write "Your Name is " & Request.Form("MYNAME") & "."
End Sub

Here is an ASP page with a Form that posts to itself, and instantiates and tests our COM+ Component:

'Complus.asp - sample asp page to instantiate and test COMPLUS.Test
if Request("SUBMIT") = "" then
With Response
.write "
.write "<INPUT TYPE=TEXT NAME=MYNAME>Enter Your Name<BR>"
.write "</FORM>"
end with
DIM obj
set obj = Server.CreateObject("COMPLUS.TEST")
set obj = Nothing
End if

That's it! That's all it takes to gain access to the ASP Intrinsics under COM+! Simply compile your component, run the COM+ Services MMC, create a new COM+ Application, and import your compiled component "COMPLUS.TEST". You should now be able to load the sample ASP page in your browser. The rest is easy - you have access to the VB IDE for debugging and Intellisense to create whatever type of custom business object you need. Er, that is until you discover how to debug them in the IDE...

There is a minor "undocumented feature" of VB COM+ debugging that you should be aware of:

When you debug a COM+ Component in Visual Basic 6.0 on a Windows 2000-based computer, the following error message may appear in the browser when the client is an Active Server Pages (ASP) page:

Server object, ASP 0178 (0x800A0005) The call to Server.CreateObject failed while checking permissions. Access is denied to this object.

This behavior only occurs when you run the COM+ Component from within the Visual Basic integrated development environment (IDE). The error does not occur when you run the COM+ Component in a compiled state within a COM+ application. The reason is that the authenticating user, usually the IUSR_ account if you are using anonymous access, does not have the appropriate permissions to access the DCOM Server (VB6.exe in this case).

Fortunately the solution is relatively simple:

1. Create an entry for VB ASP Debugging in DCOM: Start Microsoft Notepad or another text editor and type the following case-sensitive syntax:

[HKEY_CLASSES_ROOT\CLSID\{70F214BA-94E2-4bdf-8F30-32CB4A905E4D}] @="VB ASP Debugging"
[HKEY_CLASSES_ROOT\CLSID\{70F214BA-94E2-4bdf-8F30-32CB4A905E4D}\LocalServer32] @="vb6.exe"
[HKEY_CLASSES_ROOT\AppID\vb6.exe] "AppId"="{70F214BA-94E2-4bdf-8F30-32CB4A905E4D}"

Save the file as VBASPdebug.reg, and execute it by double - clicking on it.

2. Add the Everyone account to the DCOM permissions for Visual Basic ASP debugging.
Start DCOMCNFG. On the Start menu, click Run, and then type dcomcnfg in the dialog box.
On the Distributed COM Configuration Properties page, click the Applications tab, select VB ASP Debugging from the list, and then click Properties.
In the VB ASP Debugging Properties property sheet, click the Securities tab, and then click to select the Use custom access permissions check box. Click Edit.
In the Registry Value Permissions window, click Add, and then add the Everyone account for Allow Access.
Click OK, and then click Apply to apply the changes and exit the Distributed COM Configuration properties page.

Another workaround is to set the directory for the ASP Page Application settings to HIGH (Isolated), and disable Anonymous Access. Make sure Integrated Windows Authentication or Basic Authentication are checked. If Integrated Windows authentication is used, then run the client browser to access the ASP page under the same user account as the Visual Basic IDE debug session. If Basic authentication is used, enter the username and password for the same user account that the Visual Basic IDE debug session is running under.

The details of these fixes can be found in KB Q259725, "PRB: Error Occurs When You Debug a COM+ Component Under the Visual Basic IDE with an ASP Client" in the Microsoft Knowledge Base.

Another common complaint is getting "Permission Denied" or "Library Not Registered" errors when attempting to instantiate your component in an ASP page. While some of the following fixes are "undocumented", I suggest you try these:

1) The ASP ObjectContext and related utilities can be found installed in the IIS Utilities Application under COM+. Try installing your component that needs access to these in that same application right along with them.

2) Make sure that the user context that calls your component has permissions to actually launch it. What I do is go to Local Users and Groups, and set the password to the IUSER_machinename account to BLANK (no password). Then in COM+, right click on the Application (Package) your component is in and set the authentication to the IUSR account. Now any browser session should be able to run your component.

3) If all else fails, run DCOMcnfg and make sure your chosen user has launch and access permissions to your component.

This short article and example should get you off to a running start developing ASP components in Component Services.