Contents
Overview 1
From COM to COM+ 2
COM+ Architecture 16
Administering COM+ Applications 23
Lab 3.1 Building a COM+ Application 40
Debugging a COM+ Component 48
Deploying a COM+ Application 57
Lab 3.2: Debugging COM+ Components 72
Best Practices 75
Review 77
Module 3: Introduction
to COM+
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the express written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
!
Create and add a component to a COM+ application.
!
Debug a COM+ component.
!
Deploy a COM+ application.
In the labs, students will learn to create, debug, and deploy COM+ applications.
Materials and Preparation
This section provides you with the required materials and preparation tasks that
are needed to teach this module.
Required Materials
To teach this module, you need the following materials:
!
Microsoft PowerPoint file 1907A_03.ppt
!
Module 3: Introduction to COM+
!
Lab 3.1: Building a COM+ Application
!
Lab 3.2: Debugging COM+ Components
Preparation Tasks
To prepare for this module, you should:
!
Read all of the materials for this module.
!
Complete the labs.
!
Read the instructor notes and the margin notes for the module.
COM+ to manage system resources and transactions.
!
Administering COM+ Applications
Show students how they can use the Component Services tool to manage
and administer existing COM+ applications and components. Explain that
the Component Services administration tool can also be used to create
COM+ applications, add components to applications, and view information
about COM+ components. Discuss the COM+ catalog, which acts as a data
store for COM+ applications and component configuration information.
Explain how the COM+ administration objects are used by the Component
Services tool to access the catalog. Explain how these Automation objects
are also available to scripted clients, which allows the development of
automated administrative utilities.
There is a practice on creating a COM+ application and adding a component
to the newly created application.
!
Debugging a COM+ Component
There are a number of issues that need to be addressed when using
Microsoft Visual Basic to debug a COM+ component. Explain how to
configure a Microsoft ActiveX DLL project for out-of-process debugging.
Discuss some of the issues and limitations that need to be addressed when
debugging COM+ components.
Demonstrate how to debug an ActiveX DLL component within a COM+
application. Refer to the demonstration notes presented in the next section.
!
Deploying a COM+ Application
Explain how to use the Component Services tool to export COM+
applications for deployment on other servers. Students should also know
how to export application proxies for client-side deployment. Finally,
discuss the Windows 2000 Class Store, an Active Directory container used
duration of the debug session. This demonstration uses the sample BankApp
component DLL located in <install folder>\Democode\Mod3\Debugging.
!
Prepare for the demonstration
1. Create a COM+ server application called BankApp. Take all the default
options when creating this application.
2. Insert the BankApp component DLL into the application. This DLL
contains one class called CAccount, which represents a bank account.
3. Familiarize yourself with the BankApp component by loading the
BankApp.vbp project in Microsoft Visual Basic. Notice that the CAccount
class has three public routines: Deposit, Withdraw, and GetBalance.
4. Familiarize yourself with the TestHarness executable by loading the
TestHarness.vbp project in Visual Basic. The main form of the TestHarness
project displays three buttons, which call the three public routines of the
CAccount class.
Note
vi Module 3: Introduction to COM+ !
Perform the demonstration
1. Show students the BankApp application within the Component Services
tool.
2. Display the properties of the BankApp.CAccount class and illustrate the
current DLL file location prior to debugging.
3. Load the BankApp.vbp project into Visual Basic.
4. Set breakpoints within the Deposit, Withdraw, and GetBalance routines.
5. Demonstrate how to set the current debugging option to start the
TestHarness executable by using the Debugging tab of the Project
Overview
!
From COM to COM+
!
COM+ Architecture
!
Administering COM+ Applications
!
Lab 3.1: Building a COM+ Application
!
Debugging a COM+ Component
!
Deploying a COM+ Application
!
Lab 3.2: Debugging COM+ Components
!
Best Practices
!
ReviewIn this module, you will learn about COM+ services and how to use these
services to support enterprise systems development. The Component Object
Model (COM) and Microsoft Transaction Server (MTS) have been integrated to
become COM+ in Microsoft Windows 2000. COM+ provides prefabricated
infrastructure to support the middle-tier functionality of an enterprise system.
This module focuses on key COM+ services and the architecture of a COM+
application. You will learn how to create, debug, and deploy a COM+
application.
For information about COM and COM interfaces, see Appendix A:
#
##
# From COM to COM+
!
COM Programming Model
!
Using COM Interfaces
!
Implementing Interfaces
!
Microsoft Transaction Server
!
COM+ Programming Model
!
COM+ Services In this section, you will learn about the evolution of COM. You will learn how
classic COM provides a programming model and supporting run-time library,
which together assist in the creation of component-based software systems.
You will learn how MTS enhanced the COM programming model by
introducing attribute-based programming. With attribute-based programming,
COM developers can administratively tap into a rich set of infrastructure-
related services provided by MTS. In Windows 2000, COM and MTS have
been consolidated to form COM+, the latest component programming model.
Finally, you will learn how to take advantage of COM+ services to build
distributed applications.
This section includes the following topics:
!
COM Programming Model
$
EXE components can’t take advantage of COM+ servicesCOM defines a programming model and provides a supporting run-time library
that facilitates the development of software components. A component can be
thought of as a discrete unit of functionality offering a particular service or
services. In an insurance application, for example, a PremiumCalculator
component may provide a calculation service capable of generating insurance
premiums based on risk data. Another component might search for a customer’s
city and state based on the zip code entered.
The fundamental goal of COM is to enable applications to be constructed from
preassembled parts or components. Such applications are easier to maintain and
change than the so-called monolithic applications built prior to the component
era. Components provide these advantages because they can be replaced with
newer versions over time and with minimal impact on the rest of the system.
COM components also benefit from vendor independence, language
independence, and location transparency.
Developing COM+ applications in Visual Basic means that you do not have to
worry about many low-level details of COM. However, you should have a
fundamental working knowledge of basic COM concepts and terminology
before you start developing COM+ applications. For more information about
COM, see Appendix A: Understanding COM Fundamentals.
In this course, you will focus on DLL-based components because only
DLLs can take advantage of the benefits provided by COM+. Windows 2000
supports EXE-based components, but these components will not be able to
benefit from the many additional services and features provided by COM+.
Note
specifies the precise syntax of the functions used to communicate with the
object. When programming in Visual Basic, it is not always apparent that
interfaces are being used when interacting with COM objects. Visual Basic
presents a COM object as an entity exposing a set of methods, properties, and
sometimes events. Under normal circumstances, a Visual Basic client will be
connected to the default interface exposed by the COM object.
When working in the COM+ environment, it is sometimes necessary to interact
with a COM object through several of its supported interfaces. For example, the
COM+ context object discussed in this module exposes a number of interfaces
providing transaction and security-related functionality.
Sometimes, it is necessary to develop a Visual Basic class module that
implements an externally defined COM interface specified in a type library. For
example, this technique is essential when developing COM+ event classes used
with the COM+ Event service. For more information about the COM+ Event
service, see Module 5: Queued Components and Events.
Default Interfaces
The Set statement is used in conjunction with New or CreateObject to obtain
an object’s initial interface. This interface is usually the object’s default
interface, although the exact interface returned depends on the type of the
variable being set. Consider an account object exposing IAccount and ITrade
interfaces. The following illustration shows the COM object together with the
client code required to navigate between the two supported interfaces.
The above illustration shows an interface called _CtradingAccount. This
interface is the object’s default interface generated by Visual Basic when a class
module is compiled. The name of this interface is derived from the name of the
class module. In this example, the default interface contains no public functions
because the object’s functionality is exposed solely by means of the IAccount
and ITrade interfaces.
Module 3: Introduction to COM+ 5
Private Sub ITrade_Buy( ByVal lngQuantity As Long )
'Code statements
End Sub
Private Sub ITrade_Sell( ByVal lngQuantity As Long )
'Code statements
End Sub
IAccount
ITrade
Interface Definitions
within Type Libraries
ITrade
IUnknown
CTradingAccount Class Module When working in the COM+ environment, it may be necessary to create a COM
class that implements one or more externally defined COM interfaces.
To create a COM object that implements externally defined interfaces, create a
class module and then specify the Implements keyword, followed by the
interface name for each interface to be implemented. You must first establish a
reference to the type library or libraries containing definitions of the externally
defined interfaces. The above illustration shows how a class module called
CTradingAccount would implement two externally defined interfaces called
IAccount and ITrade.
When implementing an interface, you must provide implementations for all of
the functions defined in the interface.
Some clients may want to use only the IAccount interface, while other clients
may use only the ITrade interface or both interfaces. Implementing multiple
and separate interfaces allows classes to be more versatile in what features they
expose and use. For more information about the Implements keyword, see
MTS was the first Microsoft product to provide the infrastructure for enterprise
solutions. The rationale for MTS was to allow the separation of an application’s
business logic, which varies greatly from one application to another, from the
enterprise infrastructure, which essentially remains constant.
MTS enhances the COM programming model. While the underlying mechanics
of COM do not change, MTS provides an extra wrapper layer that rests on top
of COM. MTS provides an environment for hosting regular DLL-based
components. These components, typically containing business logic, can then
inherit enterprise infrastructure (such as security and transaction services)
directly from MTS.
8 Module 3: Introduction to COM+ Traditional Services
MTS represents a new type of technology that combines the features of
traditional distributed object technology with traditional online transaction
processing (OLTP) to provide the following services.
Service Description
Object Request Broker When a call comes in to a server requesting an
object, the Object Request Broker (ORB) handles
the call, checks for availability, and, subject to
security restrictions, gives the caller an object.
Surrogate Server Process MTS components must be in-process components
so that all objects can be created within MTS-
managed server processes, referred to as MTS
packages. The MTS surrogate server process uses
the MTS Executive (a DLL) to provide the run-
time support required by component-based,
MTS-provided services required at run time. Attributes are applied
administratively, often at system deployment time, by using a graphical
administrative tool called the MTS Explorer. Attributes are held within a data
store called the MTS Catalog.
Module 3: Introduction to COM+ 9 At run time, when MTS activates a component, it accesses the catalog to find
out what additional services the component requires. For example, components
that have a transactional attribute set would need to be automatically enlisted in
a transaction.
MTS provides this additional functionality through the use of a helper object
called the context wrapper. The MTS run time intercepts regular component
activation requests and inserts a wrapper object between the client and the
object. A pointer to the context wrapper is returned to the client. The client
cannot differentiate between the wrapper and the real object.
With the wrapper in place, MTS can intercept every method call made by the
client. This interception allows MTS to provide any additional functionality that
may be required based on the component’s configuration. The context wrapper
also facilitates Just In Time (JIT) Activation. Objects can be activated and
deactivated under the control of the context wrapper and transparently to the
client.
10 Module 3: Introduction to COM+ COM+ Programming Model
!
Context
$
COM+ uses contexts to group together objects with the
Context
COM+ uses a logical abstraction referred to as a context to group objects
together that share the same run-time requirements. This concept is similar to
the apartment abstraction, which existed in Windows NT 4 and still exists
under Windows 2000. However, while the apartment exists to group together
objects with similar concurrency constraints, contexts group together objects
based on their general run-time requirements. The run-time requirements of a
class are determined by the set of configuration attributes defined for the class
in the COM+ Catalog. A component’s attributes determine the type of services
it requires. These services are provided by COM+ at run time.
The collective set of attributes describing the services that a component requires
is referred to as the component’s configuration. There are two classes of
components.
Component class Description
Configured components These components have been installed into the COM+
environment and have an associated attribute set.
Nonconfigured components These components run outside the COM+ environment
and include out-of-process servers.
Module 3: Introduction to COM+ 11 In Windows 2000, processes are partitioned into contexts. Because different
classes can be configured with different requirements, a process often contains
more than one context to separate incompatible objects from one another. All
cross-context communication occurs by means of a proxy-stub pair. The proxy
is responsible for switching the run-time environment from the caller’s
configuration to the target object’s configuration. The types of operation the
proxy may need to perform include transaction enlistment, lock acquisition, and
class’s requirements during method execution. 12 Module 3: Introduction to COM+ COM+ Services
!
Concurrency Management with Activities
$
Allows you to write components from the point of view of
a single userMany of the services provided by COM+ are enhanced versions of MTS
services. COM+ also introduces a number of new services, all of which are
designed to make it easier to create enterprise applications.
Concurrency Management with Activities
When a distributed application provides services to multiple users, it can
receive simultaneous calls from clients. It can also have its business logic
running in more than one process on more than one computer. The
synchronization issues involved with managing multiple threads on multiple
computers are complex to program. Using a simple concurrency model to
develop distributed applications enables you to focus on writing the business
logic for your application rather than the synchronization code.
COM+ provides this simple concurrency model through activities. An activity
is the path of execution that occurs from the time a client creates a COM+
object until the client releases that object. During the activity, the client makes
calls to the COM+ object. To service those calls, the COM+ object may create
additional objects that are part of the same activity. The activity ensures that all
Loosely coupled publisher / subscriber architecture
!
Security
$
Based on rolesResource Management
As an application scales to a larger number of clients, objects in the application
must share resources such as network connections, database connections,
memory, and disk space, and they must use them only when necessary. COM+
provides a number of resource management features to improve scalability,
including Just in TIme (JIT) Activation, thread pooling, object pooling, and
database connection pooling.
!
Just In Time Activation
COM+ helps conserve server memory by keeping an instance of an object
alive only when a client is calling the object. This practice is known as JIT
Activation, and it allows the server to handle more clients than is possible
when the object remains active.
When a client calls a method on an object, COM+ activates the object by
creating it and allowing the client call to proceed. When the call returns and
the object finishes its work, COM+ deactivates the object by removing it
from memory. Later, when the client calls the object again, COM+
reactivates the object. From the client point of view, the object has always
been there and the object's reference has always been available.
!
Thread Pooling
As requests are received from clients, COM+ automatically assigns threads
to components from a preallocated pool. When a component finishes
For more information about COM+ resource management features, see Module
4: Managing Transactions and State.
Transactions
Work done in a distributed multiuser application can involve changes to
database tables and even nondatabase entities such as message queues.
Frequently, more than one object in an activity is initiating changes. Because
the work in any single object can fail, the application must be able to handle
situations in which some objects fail, but others succeed. Work cannot be left in
an inconsistent state in which some of the work is done, but other parts are not.
Transactions provide an all-or-nothing simple model of managing work. Either
all the objects succeed and all the work is committed, or one or more of the
objects fail and none of the work is committed. Based on the outcome, any
database tables or files affected by the work will either all be changed or none
will be changed at all (that is, they will not be left in an inconsistent state).
The programming required to manage transactions across multiple databases
and nondatabase entities is difficult to implement in an application. COM+
automatically provides this transaction management service by creating
transactions for components when they are activated. COM+ also automatically
handles cleanup and rollback of a failed transaction. When you use COM+, you
do not have to write any explicit transactional code in your components.
For more information about COM+ resource management features, see Module
4: Managing Transactions and State.
Queued Components
COM+ provides an asynchronous communications mechanism that allows
COM clients to make calls on a COM object when the object’s server computer
is not reachable on the network (for example, during disconnected operation).
The asynchronous style of communications employed by queued components is
need to make sure that only authorized users are allowed to perform important
operations. Writing security code by using the raw Windows NT security
mechanism or the security APIs provided by DCOM is complex and time
consuming.
Using the COM+ security mechanism allows you to specify your application’s
security requirements administratively without writing any code. In situations
too complex for the administrative mechanism to handle, COM+ also provides
a simple programmatic interface.
COM+ provides security by allowing you to define roles. A role defines which
users (Windows NT user accounts and groups) are allowed to invoke interfaces
on a class. You map each role to specific classes, interfaces on those classes, or
even individual methods. Then you add Windows NT users and groups to their
appropriate roles. COM+ ensures that those Windows NT users and groups can
only access the classes and interfaces to which their roles are mapped. You do
not have to write any special code in your components to handle role-based
security.
Roles map well to real-world scenarios. For example, in a banking system, roles
may include cashiers, supervisors, and managers. You can restrict access to
sensitive areas of functionality within your application to the appropriate users
based on their roles.
For more information about COM+ security, see Module 8: Making
Applications Secure.
16 Module 3: Introduction to COM+ #
##
# COM+ Architecture
!
COM+ Applications
$
A trust boundary that enables you to control security for a group of
classes.
$
A unit of administration and deployment.
!
Separate server processes provide
$
Security context for each process.
$
Fault Isolation.
!
Types of COM+ Application
$
COM+ supports library applications in addition to server applications
$
Library applications run in client’s process address space.A COM+ application consists of a collection of COM classes that are contained
within in-process components (DLLs). These classes generally contain business
logic. A COM+ application also contains the type libraries associated with its
classes. The application allows the classes to be administered and deployed as a
single unit and also provides a host process for objects at run time. A COM+
application could be described as any one of the following:
!
A group of classes that perform related tasks
!
A surrogate server process for hosting objects
!
Fault isolation.
Applications also define fault isolation boundaries. If a component in one
application fails, it can potentially result in the termination of the server
process in which it resides. It will not, however, affect components in other
applications because they are running in separate server processes. Fault
isolation is another factor to consider when determining in which
applications to put your components. Resource pooling is on a per-process basis. By grouping components that
share expensive resources such as database connections in the same application,
you can improve the scalability of your application.
The following illustration shows the architecture of the Purchase Order
application that is used in the lab scenario.
Ti
p
Module 3: Introduction to COM+ 19 Types of COM+ Applications
It is possible to run COM+ applications in the address space of the client
process. To support this feature, COM+ defines two types of application.
Type Description
Server applications A server application is a COM+ application that runs in its
own process, as shown in the preceding illustration. A
separate copy of the COM+ surrogate process, dllhost.exe, is
used for each server application. A server application can