Getting Started with COM+ - 26 Jan 2001

COM+, Windows 2000's new middleware feature, is the core of all Win2K component services. You use COM+ just as you use Microsoft Transaction Services (MTS) in Windows NT 4.0: to host your COM components and provide transaction services. I introduced some MTS features in the VB Toolkit columns "Using COM Components with MTS," InstantDoc ID 8677, and "Running a COM Component in MTS," InstantDoc ID 8841. This column takes you to the next step: showing you how to start using COM+ to host COM components that you've written in Visual Basic (VB) or other languages and that connect to SQL Server through ADO or a similar technology.

The process of building a COM+ application is similar to the process of building an MTS application: You first build a COM component, then wire it into COM+. Let's begin. First, start VB, and create a new ActiveX DLL project or open an existing ActiveX DLL project. After you've created or opened your project, select Project References, COM+ Services Type Library, as Figure 1 shows. Click OK to set the reference for the COM+ interfaces you need. Note that not all COM+ entries—such as COMPlus 1.0 Catalog Replication Type Library—have the COM+ prefix.

Next, you need to perform a little housekeeping for your component. From the Project menu, open Project Properties, and click the General tab, which Figure 2 shows. Enter a name and description for your component. To prevent naming inconsistencies, make sure you follow your organization's naming conventions. Remember that the component's name and description will appear in COM+ and anywhere else your component's interface is visible. Most developers leave the General tab's Unattended Execution check box clear, which is the default. The following explanation in the MSDN library ( identifies some things that happen when you select this check box: "An in-process component designed for unattended execution (that is, no user interaction) can be marked thread-safe. When used by a multithreaded client, it can provide objects on any of the client’s threads of execution, while retaining the performance benefits of in-process method and property calls." You use the Unattended Execution setting only when you create "headless" components, such as those that you use with COM+, COM, or MTS. When you select Unattended Execution, VB automatically removes any output from your application. For example, if you're using a message box and you enable this setting, VB won't display the box.

Selecting the Unattended Execution check box also enables the Retained In Memory setting, which retains your component in memory instead of removing it when the component is no longer in use. The Microsoft article "BUG: VB DLL Has Memory Leaks and Crashes in COM+ if 'Retain In Memory' Not Set" ( warns that not enabling the Retained In Memory setting might result in your application experiencing one of two problems: Either the application will crash with an access-violation error in msvbvm60.dll or the client using the component will enter a deadlock state and everything related to the application will stop working while the application waits on the component to complete execution. The fix for both errors is to turn on Retained In Memory before you compile the component. When you check Retained In Memory, the VB runtime and your component remain in the calling process even when the process releases component objects. In the case of Microsoft IIS, the VB runtime and your component remain in the process, which improves performance dramatically because IIS doesn't have to reload the component and the VB runtime from disk each time it instantiates an object. Visual Studio (VS) Service Pack 3 (SP3) includes an update and information about Retained In Memory. Because each service pack provides updates and fixes for known runtime problems with VB components, make sure you're running the latest service pack on both your development systems and the servers running COM+ components. After you've changed or verified the settings in the Project Properties dialog box, click OK to close the dialog box.

Next, you need to set the class’s specific COM+ properties. If you created a new ActiveX DLL project, you'll have an empty class named Class1 in your project. Open the class in the code editor by opening the project's first class, then press F4 to see the class's properties. You can still use the MTSTransactionMode property with COM+ to control the COM+ transaction type just as you do with MTS. The MTSTransactionMode property in COM+ controls whether COM+ runs the component in a new transaction or in an existing transaction. The transaction types in the MTSTransactionMode list map to the same transaction types in COM+. However, COM+ includes a new transaction type, Disabled, which means that a class doesn't need any transaction features. When you select Disabled, COM+ disables transaction features for the class so that the class can operate without that overhead. But to set COM+'s transaction support to Disabled, you must be in the COM+ interface—not in VB. To set other transaction types, simply select the appropriate type from the MTSTransactionMode list.

You can now compile your component and load it into a COM+ application. An application is to COM+ what a package is to MTS. An application is a container for one or more components and their classes and might specify a process namespace for those components. If you mark a COM+ application as a server application, the COM+ application runs in its own process space and has its own namespace. In this case, the namespace is simply the process because all element names are unique within the process.

In Win2K, the MTS Explorer is gone. The MTS Explorer's features now show up in COM+'s Component Services Explorer tool in the Administrative Tools folder. However, the process you use to create a new application works just as it does with MTS: Simply expand Component Services, Computers, and My Computer. Click the COM+ Applications folder, then right-click the folder, and select New, Application. Click Next on the first page of the wizard that appears. On the next page, click Create an Empty Application. On the resulting page, enter the application's name, then click Next. Select the user account in which you want the application to run, click Next, and click Finish to create the application.

Now you can install your component in the new application. Just right-click the Components folder in the application in which you're installing the component, and select New Component from the shortcut or context menu. Then, follow the steps in the wizard to add the component to your application. Alternatively, you can install the component in the application by opening Windows Explorer, dragging the component from the file system, and dropping the component into the Components folder. However, if you create an event class, you must use the wizard to install the class in the application because Win2K provides no context menu support when you right-drag a component into the Components folder. An event class is a class that contains code that can trigger events in your component. A COM+ event fires the same way a standard VB event fires: You supply the code to execute, and COM+ fires the event at the appropriate time.

Here's another tidbit from MSDN's COM+ documentation. When you install VB on a system with COM+, a new Component Services add-in appears on the Add-Ins menu. This add-in is a new version of the MTS add-in that installed on NT 4.0 as part of the MTS installation in the NT Option Pack. Although the Component Services add-in is supposed to automatically refresh the component in the COM+ catalog each time you compile the component, the documentation on MSDN notes that this add-in doesn't correctly refresh all the bits necessary and recommends that you shouldn't use it. However, the documentation doesn't say exactly what the add-in doesn't refresh. Not being able to use this add-in is frustrating. The add-in would save a lot of time that would otherwise be spent using VB to build components for COM+. Because you can’t use the same add-in functionality with COM+ that you use with MTS, you'll have to spend a lot more time on your development projects.

COM+ has an administrative interface just as MTS does. Investigate this interface, and learn how you can use it to automate the COM+ tasks that your own applications are running. The interface in COM+ is the COM+ 1.0 Admin Type Library. For example, I used the MTS Admin Type Library to create a utility that lets me use one keystroke to shut down any package. You can create the same type of utility with COM+ or create a utility that automatically refreshes the component in COM+ for you.

Stay Tuned

COM+ brings many benefits to both SQL Server developers and systems administrators. Developers don't need to write low-level transaction code in either T-SQL or VB. COM+ does it for you. COM+ also handles the startup and shutdown of your components, providing a performance boost to your applications. In addition, COM+ provides administrative features that you can use to both configure and monitor components. For example, you can monitor the components that are running in COM+ and see which transactions are active or pending. You can also see statistics such as the number of transactions in process and related information. Use this article to get started with COM+, then start exploring how this technology can help you build better database applications faster.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.