This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.

MIND

Compiling Components in Visual Basic for ASP

Ken Spencer

Code for this article:Serving0300.exe(32KB)

T

here are lots of little issues that come into play when you begin to design and build software around any set of technologies. After all, designers can't know how each developer will try to use their technologies in an application. That's one reason why you face so much trial and error when you build applications.
      In this column I'll give you some tips for buildingâ€"and more specifically, compilingâ€"components to be used with Microsoft® Transaction Server (MTS) and Internet Information Server (IIS), so that you can avoid some common pitfalls during your own development process.

ASP Components

      First, let's talk about what happens when you need to recompile a component that is being used in an ASP application. When you use a component from within your ASP code, you cannot delete, replace, or recompile the component until you do one of two things. If the ASP application is running in-process with IIS, you must shut down IIS before the file is modified. If the ASP application is running out-of-process, you can click the Unload button on the application's Directory Properties page. Once you have either stopped IIS or unloaded the application, you can delete, replace, or recompile the component.
      Each of these processes is a hassle because most people start the Internet Service Manager and use it for both tasks. There is an easier way to stop and restart IIS. You can perform both of these tasks from the Command Prompt using the Net command. I created two small command files that stop and restart IIS for me.
      When I need to stop IIS, I run the following command file:


IISStop.cmd
net stop iisadmin /yes

This stops the iisadmin service, which will in turn stop the other Web services. The iisadmin service must be stopped before you can replace a component that has been used in an ASP page.
      To restart IIS, I run this command file:


IISStart.cmd
net start iisadmin
net start w3svc
net start smtpsvc

This file restarts the iisadmin, w3svc, and smtpsvc services. You must restart each one explicitly because even though stopping iisadmin stops the others, restarting iisadmin will not restart the other services automatically.

MTS Components

      Now let's take a look at MTS and how it applies to your applications. I do most of my development on my workstation. When the component is ready for more extensive testing, I publish a copy of it on a server for testing. Developing on my workstation saves time because I can start and stop services without bothering anyone else on the network. I've also discovered an MTS add-inâ€"the Visual Basic® Transaction Server Add-inâ€"that lets you automatically refresh a component in an MTS package after the component has been recompiled. This is a big deal because now you don't need to delete and reinstall the component in MTS after you compile it. The add-in also allows you to refresh all of the components in a package.
      The MTS add-in is not installed by default when you install the Windows® NT Option Pack. To install it, start the Windows NT Option Pack setup. Click the Add/Remove button, then select Transaction Server from the list of components and click Show Components. Next, select Transaction Server Development and click Show Components once more. Finally, make sure the Visual Basic Transaction Server Add-in is selected, as shown in Figure 1. Once you have selected the add-in, click OK and finish the installation.
Figure 1 Installing the MTS Add-in
Figure 1Installing the MTS Add-in

      Once you have the add-in installed, it will show up in the Visual Basic Add-Ins menu. The add-in is enabled by default and automatically set to refresh components, as shown in Figure 2. You can toggle this behavior on or off by selecting the first menu option.
Figure 2 AutoRefresh Components
Figure 2AutoRefresh Components

      Now, for completeness, let's walk through the process of using a component with MTS. First, open your project in Visual Basic. Create a project reference to the Microsoft Transaction Server Type Library, then add in support for the MTS context object by adding the following code to the top of a procedure:


Dim Ctx As ObjectContext
Set Ctx = GetObjectContext

Now your component can interact with MTS as needed using SetComplete, SetAbort, or other MTS features.
      Before you go any further, open the properties for the project and click the Components tab. Make sure you set the compatibility property to either Project or Binary. You can usually use Project during your testing. If you do not set the compatibility property, you will run into problems when you recompile the component because a new GUID will be generated each time. This will prevent MTS from finding the newly compiled component and will lead to the following error when you try to use it:


Microsoft VBScript runtime error '800a01ad'
ActiveX component can't create object: 'MTSDB.Recordset'
/MIND/MTSDriver.asp, line 31

      If you get this error, you can take a look at what MTS is doing with Task Manager. If you sort the processes by image name, take a look at MTX.EXE. You should see one of these processes running whenever you are using IIS. If MTS tries to load a component and can't, you will see a new MTX.EXE for that package start and quickly disappear. Then the aforementioned runtime error will appear in the browser. Setting the compatibility property for the project as suggested will prevent this.
      Next, you can set the MTS transaction type for your classes. You can set the transaction type in either MTS Explorer or in Visual Basic. I prefer to set it in Visual Basic because this compiles it into the component and makes sure it is set correctly every time you install the component in MTS. To set the transaction type, select the class you are working with in the Visual Basic Project Explorer. Next, select the correct transaction type in the properties window. You can set the transaction type according to the table in Figure 3 (pulled from the MTS docs).
      Once you have set the transaction type, you can compile the component and install it in MTS. To do this, you will need an MTS package. To create a new package, start MTS Explorer and select the Packages Installed folder. Right-click Packages Installed and select New, then Package. Next, click "Create an empty package." Enter the package's name and click Next. If you are building a package on your workstation for testing, you can leave the security set at Interactive user and then click Finish.
      If this package will be used on a server, you should set the identity for the package to a valid account. I suggest that you create an account specifically for use by MTS and assign it the permissions and rights it needs. Then you can assign this account to the MTS package, as shown in Figure 4.
Figure 4 Setting the Package Identity
Figure 4Setting the Package Identity

      You can enter the identity for a package either when you create it or when you use it later. To add the identity for the package later, open the properties for the package in MTS Explorer and select the Identity tab. You will need the password for the account that you assign to your package.
      Now you can install the component into MTS. Drag the component from Windows Explorer and drop it into the MTS packages Components folder. You can also install the component into a package by selecting New from the context menu of the package's Components folder, then following the prompts. If you have any problems installing a package by dragging and dropping, you can use the menus in MTS Explorer.
      Now that your package has been installed in MTS, you can execute an application that uses it and begin your testing. What happens with MTS when you need to recompile the component? Well, that's when it starts to get interesting. If the package is running as a Server package (meaning it runs in its own process space), you do not need to shut down IIS. You can simply shut down the package itself. Do this by opening MTS Explorer, right-clicking the package, and selecting shut down from the shortcut menu. This will kill the copy of MTX.EXE that is hosting the package, allowing you to recompile the component.
      Sometimes it will seem like you cannot recompile the component because something is holding it open. If you are using Visual InterDev® and have a page open that uses the component, you cannot recompile until you close the page. That's because Visual InterDev is holding a reference to the component and will not allow you to recompile until that reference is gone.

Automating MTS Package Shutdown

      It is cumbersome to continually shut down the MTS package to recompile a component. It's easier than shutting down IIS, but still requires flipping over to MTS Explorer and so forth. Luckily, the MTSAdmin object's interface allows you to automate this process. I hacked together some Visual Basic code that will allow you to shut down a package on your local system (see Figure 5 and Figure 6).
       Figure 5 shows the code for the application. If you run the app without any command-line parameters, frmMTSUTilInferface will display a list of MTS packages. Selecting a package will turn on the Shutdown Package button, as shown in Figure 7. Clicking this button will shut down the selected package.
Figure 7 Shutting Down a Package
Figure 7Shutting Down a Package

      You can also execute the application with a parameter to shut down a package automatically. The shortcut looks like this:


MTSUtil.exe \Shutdown MindMTS

This command will shut down the MindMTS package. To work faster, you can create an icon on the taskbar for each of the packages that you are working on. Then you can simply edit the icon's properties and specify the associated package name as I did in the shortcut I just mentioned. Clicking that icon on the taskbar will then shut down the package.
      The code shown in Figure 6 does all the work using the MTS Admin object. To use these functions, you should create a project reference to the MTS 2.0 Admin Type Library when you're working on the code in Visual Basic.
      You can also compile Visual Basic-based applications and components from the command line. To compile from the command line, you supply Visual Basic with the following syntax:


"c:\program files\microsoft visual studio\vb98\vb6.exe"
/make MTSDB.vbp

This command launches Visual Basic from the command line with the /make parameter and compiles the MTSDB component. The entire command should be on one line, and you should remember to put quotes around the executable's long file name.
      To make compiling a simpler process, you could add functions to the MTSUtil.vbp app to automatically compile the components in a package after the package is shut down. You can use the Shell command in Visual Basic. Then all you need to do to recompile is save changes to the component and click the icon. That does all the work.

Conclusion

      As you can see from my discussion of building components for IIS and MTS, even the seemingly straightforward process of changing and recompiling a component can create unforeseen problems. But if you know about common compile time hassles beforehand, you'll save yourself lots of time and aggravation when building your components.

Ken Spencer works for the 32X Tech Corporation (https://www.32X.com), which specializes in conducting high-quality seminars on Microsoft technology. You can reach Ken at kenspencer@32x.com.

From the March 2000 issue of MSDN Magazine.