In "Using COM Components with MTS," I demonstrated how to build a Vendor COM component. In this article, I’ll show you how to turn it into a component that runs in MTS.
You’ll find the files for this project in the 8677.zip file in the "Download the Code" box at the top of the page. Extract these files into a folder named RawCode. Extract the files from the Project1.zip file and place them into a folder called Project1. Then follow these steps to create the folder structure for the application and add the proper files:
- Create a new folder named DBCode for the database files.
- Move recordset.bas and password.bas from the RawCode folder into the DBCode folder.
- Create a new folder named MTSCode for special MTS-related files.
- Move MTSContext.bas into the MTSCode folder.
- Create a new folder named VendorMTS.
- Add the remaining files from RawCode to this folder.
Next, you need to modify the code in the Vendor component so it will work with MTS. First, add the recordset.bas and password.bas files. These files were part of the DBStuff project, which I demonstrated how to compile into a dll last month. This time, we’ll move these files into the project as code modules. Why do you want to use code modules instead of COM components? MTS can let a component be registered in one package on a given system, which creates a problem for general-purpose modules such as the DB components. If you want to use the DB functionality in another component that runs in a different package, you can execute the methods from a component in a different package but that procedure adds overhead to the process. Moving the functionality into a code module makes it easy for you to incorporate the code into any COM component project. The downside is that without proper controls, developers can modify the code.
Follow these steps to modify the Vendor project to work with MTS.
- Open Vendor.vbp.
- Add the recordset.bas and password.bas files to the project. Select Add Module from the Project menu, then click the Existing tab. Navigate to the DBCode folder and select the files.
- Add the MTSContext.bas module to the project. Select Add Module from the Project menu, then click the Existing tab. Navigate to the MTSCode folder and select the file.
- Add a reference to the Microsoft Transaction Server Type library. Select References from the Project menu and select Microsoft Transaction Server Type library from the list, as Figure 1 shows.
- Remove the project reference to DBStuff. Select References from the Project menu and deselect DBStuff. Change the compatibility setting to point to the DLL for this project. Open the project properties and click the Component tab. You’ll see the dialog box that Figure 2 shows. Enter the path to the DLL that you’re going to replace in the textbox at the bottom of the dialog box.
Next, you need to modify the code so that it runs by using the code modules (password.bas and recordset.bas) instead of using the DBStuff component. The steps also cover setting properties on the class and integrating the MTS methods into the application:
Follow these steps to modify the code:
- Remove the lines that reference DBStuff from each procedure. This code looks like this:
Dim oDB As DBStuff.RecordsetStuff
- Change the lines that use RecordsetStuff methods. This code needs to call the methods as functions because the code from RecordsetStuff is now in a code module. You can delete the lines that instantiate an object from DBStuff. The lines that call the RunSQLWithRS method will look like:
Set oRS = RunSQLWithRS(sSQL)
- Change the instancing property for the Publisher class to 2 – Requires Transaction.
- Change all lines that use CreateObject to use the ContextCreateObject function from the MTSContext class.
- Open the Publisher class.
- Add a call to the ContextSetComplete function just before the Exit Function statement. Repeat this for each method in Publisher.
- Add a call to the ContextSetAbortfunction just after the error handler label. Repeat this for each method in Publisher.
- Recompile the Vendor component, then select Make from the File menu.
Next, follow these steps to create the MTS package and add the component to the package:
- Open the MTS Explorer in the Microsoft Management Console.
MTS Explorer is in the NT Option Pack folder on the Program Files menu.
- Open the Computers folder.
- Open the Packages Installed folder under My Computer.
- Select New Package from the Action menu, or right-click on the Packages Installed folder and select New, Package. Either of these actions will start the Package Wizard.
- Select Create an Empty Package. You have two options for creating new packages. You can install an existing package (exported from another system), or you can create a new package and enter its attributes as you go.
- Enter a name for the new package Vendor.
- Select the user account this package will run in. This option lets you specify the user account the package will run under. You usually create a user account for each package that is executing on the system and assign relevant rights to that user. For now, just accept the default (interactive user) and click Finish.
- Open the properties of the new package by right-clicking on the package in the Packages Installed folder and selecting Properties
- Browse through the tabs and available options.
- Close the Properties window.
- Expand the Vendor package folder.
- Click the Components folder.
- Use NT Explorer to drag Vendor.dll into the Components folder.
Now, follow these steps to test the component:
- Create a new folder named Project1.
- Copy Form1.frm, Project1.vbp, and Project1.vbw to the Project1 folder.
- Switch back to VB.
- Select Add Project from the File menu.
- Click the Existing tab and select Project1 from the Project1 folder you just created.
- In Project Explorer, right-click Project1 and select Set as Startup.
- Change the reference to the Vendor component to point to your newly recompiled file. Select References from the Project menu and select Microsoft Transaction Server Type library from the list, as Figure 3 shows.
MTS and VB make a powerful team for building high-performance, manageable client/server or Web applications, and they offer significant benefits in object and connection management.