Tải bản đầy đủ (.pdf) (385 trang)

o'reilly - com and .net component services

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.65 MB, 385 trang )


2

COM and .NET Com ponent Services

Dedication

Foreword

Preface
Scope of This Book
Som e Assum pt ions About t he Reader
Definit ions and Text Convent ions
Ot her COM+ Books and References
How to Contact Us
Acknowledgm ent s

1. COM+ Com ponent Services
1.1 COM+ Com ponent Ser vices
1.2 The Com ponent Services Explorer
1.3 Hello COM+
1.4 COM+ Configured Com ponent s
1.5 Applicat ions, DLLs, and Com ponent s
1.6 Configur ing COM+ Applications
1.7 Debugging COM+ Applicat ions
1.8 Deploying COM+ Applications
1.9 Sum m ary

2. COM+ Cont ext
2.1 Encapsulat ion via Marshaling in COM
2.2 Encapsulat ion via I nt ercept ion in COM+


2.3 The Cont ext Object
2.4 The Call Obj ect
2.5 Cross- Cont ext Manual Marshaling
2.6 Sum m ary

3. COM+ I nst ance Managem ent
3.1 Client Types
3.2 I nstance Managem ent and Scaling
3.3 Obj ect Pooling
3.4 Just - in-Tim e Act ivation
3.5 Com bining JITA wit h Obj ect Pooling
3.6 Obj ect Const ruct or String
3.7 COM+ I nst ance Managem ent Pitfalls

4. COM+ Transactions
4.1 Transact ion Basics
4.2 Transact ion Properties
4.3 Transact ion Scenarios
4.4 COM+ Transact ions Archit ecture
4.5 Configur ing Transact ions
4.6 Vot ing on a Transaction
4.7 Transact ional Obj ect Life Cycle
4.8 Designing Transact ional Com ponent s
4.9 Nont ransactional Clients
4.10 Transact ions and Obj ect Pooling
4.11 Com pensating Transactions
4.12 Transact ion Execut ion Tim e
3
4.13 Tracing Transact ions
4.14 I n-Doubt Transactions

4.15 Transact ion St atist ics
4.16 COM+ Transactions Pitfalls

5. COM+ Concurr ency Model
5.1 Obj ect - Oriented Program m ing and Multiple Threads
5.2 Apart m ent s: The Classic COM Solution
5.3 Act ivities: The COM+ I nnovat ion
5.4 COM+ Configuration Set t ings
5.5 Act ivities and JITA
5.6 Act ivities and Transact ions
5.7 Tracing Act ivit ies
5.8 The Neutral Threaded Apart m ent
5.9 Sum m ary

6. Program m ing t he COM+ Catalog
6.1 Why Program the Cat alog?
6.2 The Cat alog Progr am ming Model
6.3 Catalog St ruct ur e
6.4 I nt eract ing w it h t he Catalog
6.5 Features of COMAdm inCat alog
6.6 The COM+ Catalog and Transact ions
6.7 Sum m ary

7. COM+ Securit y
7.1 The Need for Securit y
7.2 Basic Securit y Term s
7.3 Role- Based Securit y
7.4 Securing a Server Applicat ion
7.5 Securing a Library Applicat ion
7.6 Program matic Role- Based Security

7.7 Security Boundaries
7.8 Advanced COM+ Securit y
7.9 COM+ Securit y Pit falls
7.10 Sum m ary

8. COM+ Queued Com ponent s
8.1 Major Benefits of Queued Com ponent s
8.2 Queued Com ponent s Archit ect ure
8.3 Com ponent Services Explorer Configurat ion
8.4 I nvoking Queued Com ponents on the Client Side
8.5 Designing Queued Com ponent I nt erfaces
8.6 Receiving Out put from a Queued Com ponent
8.7 Queued Com ponent Error Handling
8.8 Queued Com ponent s and Transact ions
8.9 Synchronous Versus Asynchronous Com ponent s
8.10 Queued Com ponents Security
8.11 Queued Com ponents Pit falls
8.12 Sum m ary

9. COM+ Event Service
9.1 Classic COM Event s
9.2 COM+ Event Model
9.3 The Event Class
9.4 Subscript ion Types
9.5 Delivering Event s
4
9.6 Event Filtering
9.7 Distribut ed COM+ Event s
9.8 Asynchronous Event s
9.9 COM+ Event s and Transact ions

9.10 COM+ Events and Securit y
9.11 COM+ Events Lim it ation
9.12 Sum m ary

10. .NET Serviced Com ponent s
10.1 Developing Serviced Com ponent s
10.2 .NET Assem blies and COM+ Applications
10.3 Regist ering Assem blies
10.4 Configur ing Serviced Com ponents
10.5 Applicat ion Activat ion Type
10.6 The Descript ion At t ribute
10.7 Accessing t he COM+ Cont ext
10.8 COM+ Cont ext At t ributes
10.9 COM+ Object Pooling
10.10 COM+ Just - in-Tim e Act ivat ion
10.11 COM+ Const ruct or St r ing
10.12 COM+ Transact ions
10.13 COM+ Synchronization
10.14 Program m ing t he COM+ Cat alog
10.15 COM+ Security
10.16 COM+ Queued Com ponents
10.17 COM+ Loosely Coupled Events
10.18 Sum mary

A. The COM+ Logbook
A.1 Logbook Requirem ent s
A.2 Log File Exam ple
A.3 Using the Logbook
A.4 Configuring t he Logbook
A.5 How Does t he Logbook Work?

A.6 Sum mary

B. COM+ 1.5
B.1 I m proved User I nterface Usabilit y
B.2 Legacy Applications and Com ponent s
B.3 Disabling Applications and Com ponent s
B.4 Pausing Applicat ions
B.5 Service Activat ion Type
B.6 I m proved Queuing Support
B.7 Application Pooling and Recycling
B.8 Application Dum p
B.9 Application Partitioning
B.10 Aliasing Com ponent s
B.11 Configurable Transact ion I solat ion Level
B.12 I m proved Cont ext Act ivat ion Set t ing
B.13 Private Com ponent s
B.14 Web Serv ices in COM+ 1.5
B.15 Sum m ary

C. I nt roduction to .NET
C.1 .NET Program m ing Languages
C.2 Packaging .NET Com ponent s: Assem blies
C.3 Developing .NET Com ponent s
5
C.4 Writing .NET Client - Side Code
C.5 .NET as a Com ponent Technology
C.6 Com posing Assem blies

Colophon


6
Dedicat ion
To m y wife, Dana
7
Forew ord
I first ran into COM+ back in 1996. I n those days, I was working as
a Com m on Obj ect Request Broker Architect ure ( CORBA) consultant
and was fresh out of I BM, where I had been heav ily involved in
I BM’s original CORBA im plem entat ion.
CORBA was the first of the archit ect ures that we m ight describe
today as Dist ribut ed Com ponent archit ect ures, which set the stage
for both COM/ DCOM in the Microsoft space and RMI / I I OP in t he Java
space.
Back t hen, I was int erest ed in a particularly knot ty problem related
to distributed com ponent archit ect ures. System s built wit h such
architectures had a characteristic perform ance pattern. They could
handle large num bers of t ransact ions, as long as t hose transact ions
originated from a sm all num ber of client s. So, for exam ple, 5,000
transact ions per m inute divided between 5 clients worked fine. But
when those sam e 5,000 transactions per m inut e were split am ong
1,000 client s, each processing 5 t ransact ions per m inut e, t he
system s choked.
This was odd, I t hought. Why should 5 clients, each processing
1,000 transactions per m inut e, be fundam ent ally different than
1,000 client s, each processing 5 t ransact ions per m inut e? What is
the difference bet ween the first 5,000 tr ansactions per m inut e and
the second?
Dist ributed com ponent archit ect ures, as they existed in 1996,
dict at ed a one- to- one relationship bet ween client s and com ponent
inst ances. The business logic of such architect ures is in the

com ponent instances. And it is the business logic that makes
transact ional request s of transactional resources, such as t he
dat abase. In order t o m ake transact ional requests, the com ponent
inst ances require expensive resources, such as database
connections. We run out of steam ( i.e., t ransact ional throughput )
when one of two t hings happen: we overload the syst em wit h
transact ional request s or we run out of resources ( e.g., database
connections) .
Clearly, going from 5 client s, each m ak ing 1,000 transactional
request s per m inute, t o 1,000 clients, each making 5 transact ional
request s per m inute, has no overall im pact on t he t ransact ional
throughput. Therefore, the reason why our distribut ed com ponent
system s m ust be dying is t hat we ar e running out of resources.
So the answer to getting lot s of client s on a distributed com ponent
architecture is not going to com e from increased capability of the
back- end transact ional resources ( e.g., databases) . I t will have to
com e from som et hing else- som et hing that allows resource sharing.
This, t hen, is the problem I worked on back in 1996. How do you
8
get several clients t o share resources in a distributed com ponent
architecture?
The solut ion to this problem cam e from an unex pect ed source. I
was asked to write a book on COM and DCOM. I knew very litt le
about COM and DCOM back t hen. As I looked over t he COM/ DCOM
white papers on t he Microsoft web site, I quickly recognized it as a
typical dist ribut ed com ponent archit ect ure and predicted the sam e
throughput problem s I had seen in other distributed com ponent
system s.
As I browsed through the whit e papers, I not iced an obscure bet a
product called Microsoft Transact ion Server ( MTS) . At first, I

dism issed MTS as an API used to manage dist ribut ed t ransactions.
But as I read m ore about MTS, I realized that it had litt le to do with
transact ions. I nst ead, it at tacked a m uch m ore interesting problem :
how to share resources am ong clients. I n a nut shell, MTS addressed
the very problem that had so vexed the existing dist ribut ed
com ponent system s-how to support a large num ber of low-
transact ion generat ing clients!
I did eventually w rit e that book, as well as m any articles on the
im portance of the ideas int roduced by MTS. Many of t hese articles
appear ed in m y ObjectWat ch newslet ter (available at
www.obj ectwatch.com ), a newslet ter t hat has, over t im e, becom e
influential in its space.
Back in 1996, I predicted t hat MTS would be a hist orically im portant
product- one t hat w ould redefine approaches to scalabilit y in
dist ribut ed com ponent system s. I n fact , t hat prediction has com e
true. Today, every infrast ructure designed to support high
scalability in distributed com ponent system s is based directly on the
ideas, algorithm s, and principals first introduced by MTS in 1996.
Enterprise JavaBeans, for exam ple, the Jav a scalabilit y
infrastruct ure, is alm ost a direct copy of MTS.
But what does this have t o do with COM+ , you may ask. I t t urns
out that COM+ and MTS are one and the sam e. Microsoft, never
known for its m arketing savvy, decided to wait unt il cust om ers
finally got used to t he nam e MTS ( itself a m isleading nam e), and
then it pulled a fast one- it switched the nam e! And not just any
nam e, but one t hat would be as confusing as possible! So they
renam ed MTS as COM+ . Naturally, cust om ers assum ed that COM+
was the next release of COM. I n fact , COM+ was t he next release of
MTS.
Now Microsoft has announced .NET. Once again, the brilliant

Microsoft m ark et ing organization has left m any custom ers confused.
I s COM+ now dead? Far from it —.NET is a series of int er esting new
features, none of which replace COM+ . COM+ is st ill the scalable
infrastruct ure that supports resource sharing and deals with the
m yriad of issues (such as security and transaction boundary
9
m anagem ent) that are so closely relat ed t o resource sharing and so
crucial t o dist ributed applicat ions.
So whether you are rushing into Microsoft ’s new .NET t echnology
platform or taking a wait and see att it ude, if you need to put a lot of
clients around your system , you need to understand COM+ .
Therefore, this book is very tim ely. COM+ is going to be with us for
a long time. I ts nam e m ay change again, j ust to confuse the
innocent; but the ideas, algorit hm s, and principals will not . COM+ ,
under whatever nam e, is here to stay!
Roger Sessions,
CEO, Object Watch, I nc.
Publisher, Obj ect Watch newslet ter ( www.objectwatch.com )
Author, COM+ and the Bat tle for t he Middle Tier
Austin, Texas
10
Preface
This book discusses COM+ com ponent services. Each service is
covered in it s own chapter, and each chapter discusses a sim ilar
range of issues: the problem the serv ice addresses, possible
solutions t o t hat problem , an in- depth description of the COM+
solution, tradeoffs, design, and im plem entation guidelines, tips, and
known pitfalls. I have tried t o provide useful design inform ation and
lessons I learned while applying COM+ . I also descr ibe COM+ helper
classes and ut ilities I developed that will enhance your productivit y

significantly . ( The COM+ Events helper object s and t he COM+
Logbook are prim e ex am ples.) This book focuses on the "how to"—
that is, it provides practical inform at ion. You should read the
chapt ers in order, since m ost chapt ers rely on inform ation discussed
in t he preceding chapters. The book also aim s to explain COM+ step
by step. A soft ware engineer already fam iliar with COM who wants
to know what COM+ is and how to use it can read this book and
st art developing COM+ applications im m ediately.


Scope of Th is Book
Here is a brief sum mary of the chapters and appendixes in this
book:
• Chapter 1 introduces t he Com ponent Services Explorer and
basic COM+ term inology. This chapter deliberately holds your
hand as you develop your first " Hello World" COM+
com ponent. Subsequent chapters do m uch less handholding
and assum e y ou are fam iliar with the COM+ environment . I f
you already have experience wit h basic COM+ developm ent,
feel free to skip t his chapt er.
• Chapter 2 dem ystifies the COM+ context by presenting it as
the key mechanism for providing com ponent services using
call int erception. Generally , you need not be concerned with
cont ext s at all. However, t he COM+ context underlies the way
COM+ services are im plem ented.
• Chapter 3 describes t wo scalability- enabling m echanism s t hat
COM+ provides for a m odern ent erprise application: obj ect
pooling and Just- in- Tim e Act iv at ion ( JITA) . The discussion of
inst ance m anagem ent, and especially JI TA, is independent of
transact ions. Early COM+ docum entat ion and books t ended t o

couple inst ance m anagem ent and transactions. However, I
found that not only can you use instance m anagem ent
independent ly of t ransact ions, but it is easier to explain it that
11
way. Besides explaining how t o best use object pooling and
JITA, Chapt er 3 describes ot her act iv at ion and instance
m anagem ent COM+ services such as the const ructor string.
• Chapter 4 explains t he difficult , yet com m on, problem s that
transact ions address, and provides you with a distilled
overv iew of t ransact ion processing and the t ransaction
program m ing m odel. The difficult part of writ ing t his chapt er
was finding a way t o convey the right am ount of transaction
processing t heory. I want to help you underst and and accept
the resulting program m ing model, but not bury you in the
det ails of t heory and COM+ plum bing. This chapt er focuses on
COM+ transaction architect ure and t he result ing design
considerations you have to be aware of.
• Chapter 5 first explains the need in the com ponent world for a
concurrency m odel and the lim itations of the classic COM
solution. I t then describes how the COM+ solution, activit ies,
im proves deficiencies of apartm ent s.
• Chapter 6 shows how t o access com ponent and applicat ion
configuration inform ation program matically using t he COM+
Catalog interfaces and obj ects. Program m atic access is
required when using som e advanced COM+ serv ices and to
aut om ate setup and dev elopment tasks. This chapt er provides
you with com pr ehensiv e cat alog st ruct ure diagram s, plenty of
sam ple code, and a handy utilit y.
• Chapter 7 explains how t o secure a m odern application using
the rich and powerful ( yet easy t o use) security infrastruct ure

provided by COM+ . This chapt er defines basic security
concepts and shows you how to design security int o your
application from the ground up. You can design this security
by using COM+ declarat ive security via the Com ponent
Serv ices Explorer and by using advanced program mat ic
security .
• Chapter 8 explains what COM+ queued com ponents are and
how to use them to develop asynchronous, potentially
disconnect ed applicat ions and com ponents. I n addit ion to
showing you how t o configur e queued com ponent s, this
chapt er addresses required changes to t he program m ing
m odel. I f you have ever had to dev elop an asynchronous
m ethod invocation option for your com ponent s, you will lov e
COM+ queued com ponent s.
• Chapter 9 covers COM+ loosely coupled events, why there is
a need for such a service, and how the service ties int o other
COM+ services described in earlier chapt ers ( such as
transact ions, securit y, and queued com ponents) . Many people
consider COM+ events their favorite service. I f you have had
to confront COM connect ion points, you will appreciat e COM+
Events.
12
• Chapter 10 shows how .NET com ponent s can take advantage
of the com ponent services described in t he previous chapters.
I f you are not fam iliar with .NET, I suggest you read Appendix
C first—it contains an introduct ion to .NET and C# . Chapter
10 repeats in C# m any of the C+ + or VB 6.0 code sam ples
found in earlier chapters, showing you how to im plem ent
them in .NET.
• Appendix A helps you develop a useful and im port ant utility —

a flight recorder that logs m et hod calls, errors, and events in
your application. Logging is an essential part of every
application and is especially im portant in an enterprise
env ironm ent. The logbook is also an excellent exam ple of the
synergies arrived at by combining m ultiple COM+ serv ices. I t
is also a good representation of the design approaches you
m ay consider when com bining services.
• Appendix B describes the changes, im provem ents, and
enhancem ents int roduced t o COM+ in the next release of
Windows, Windows XP. I nst ead of writing t he book as if
Windows XP were av ailable now ( as of this writ ing it is only in
bet a), I chose t o write the book for t he developer w ho has to
deliver applicat ions t oday, using Windows 2000. When you
st art using Windows XP, all you need t o do is read Appendix
B—it contains the additional inform at ion you need.
• Appendix C describes the essent ial elem ent s of the .NET
fram ework, such as t he runt im e, assem blies, and how t o
develop .NET com ponents. The appendix allows a reader who
is not familiar wit h .NET t o follow Chapter 10.


Som e Assum pt ions About t he Reader
I assum e t hat you are an experienced COM developer who feels
com fortable wit h COM basics such as interfaces, CoClasses, and
apartm ent s. This book is about COM+ com ponent services, not t he
com ponent t echnology used to develop a COM/ DCOM or .NET
com ponent. You can still r ead t he book without this experience, but
you will benefit m ore by having COM under your belt . I assum e you
develop your com ponents most ly in C+ + and ATL and that you
write occasional, sim ple client code in Visual Basic. I also use trivial

C# in Chapt er 10 to dem onstrate how .NET t akes advantage of
COM+ services, but you don't need to know C# t o read that
chapt er. A .NET developer should also find this book useful: read
and understand the services in Chapter 1 t hr ough Chapt er 9, and
then use Chapter 10 as a reference guide for the syntax of .NET
at tribut es.

13

Definitions and Text Conventions
The following definit ions and conventions apply thr oughout this
book:
• A com ponent is an im plem entation of a set of interfaces. A
com ponent is what you m ark in your I DL file (or type library )
wit h CoClass or a class in C# .

An object is an inst ance of a com ponent. You can creat e
object s by calling CoCreateInstance( ) in C+ + , specifying
the class I D ( the type) of the obj ect you want to create. I f you
use Visual Basic 6.0, you can create object s using new or
CreateObject( ). A C# client uses new to create a new
inst ance of a com ponent.
• I use t he following t erm s in the book: CoCreat ing refers to
calling
CoCreateInstance()
in C+ + , or
new
or
CreateObject( ) in Visual Basic. Querying an obj ect for an
interface refer s to calling IUnknown::QueryInterface( ) on

the obj ect . Releasing an object refers t o calling
IUnknown::Release( ) on t he obj ect .
• The graphical not ations in Figure P-1 are used in alm ost every
design diagram in t he book. The " lollipop" denotes an
interface, and a m et hod call on an interface is represented by
an arrow beginning with a full circle.
Figu re P- 1. I nterfa ce and m et h od call graph ical notations

• Err or handling in the code sam ples is rudim ent ary. The code
sam ples serve to dem onstrate a design or a technical point,
and cluttering t hem wit h t oo m uch error handing would m iss
the point . I n a production environm ent, you should ver ify the
returned HRESULT of every COM call, cat ch and handle
except ions in C# , and assert every assum pt ion.
I use t he following font conventions in this book:
• I talic is used for new term s, cit at ions, online links, filenam es,
directories, and pathnam es.
• Constant width is used to indicat e com m and-line com put er
output and code exam ples, as well as classes, const ants,
funct ions, int erfaces, m ethods, variables, and flow- controlled
st at em ents.
14
• Constant-width bold is used for code em phasis and user
input.
• Constant-width italic is used to indicate replaceable
elem ents in code st at em ents.

This icon indicates a note or t ip.



This icon indicates a warning.



Ot her COM + Book s and References
This book describes how to use COM+ com ponent services in y our
application. I t focuses on how t o apply the technology, how to avoid
specific pitfalls, and design guidelines. I f you want t o know more
about COM+ in general and the nature of com ponent technology, I
recom mend the following t wo books that helped me a great deal in
m y att em pt t o grasp COM+ .
COM+ and t he Bat tle for t he Middle Tier by Roger Sessions (John
Wiley & Sons, 2000) is hands down the best "why" COM+ book. I t
explains in det ail, w ith excellent exam ples and in plain language,
the need for software com ponent s and com ponent services. For
example, inst ead of the page or t wo t his book includes on the
m otivat ion for using transact ions, Sessions devotes two fascinating
chapt ers to the topic. The book goes on to com pare exist ing
com ponent t echnologies ( such as COM, CORBA, and Java) and their
corresponding suites of com ponent services. I t also cont ains a few
case studies from real- life systems that use COM+ . Roger Sessions
also has a unique way of eloquent ly nam ing things— providing the
m ost appr opr iate term , which is often not t he nam e Microsoft uses.
Whenever it m akes sense, this book uses Sessions' t erm inology,
such as " inst ance m anagem ent" instead of the Microsoft term
"act ivation."
Underst anding COM+ by David S. Platt ( Microsoft Press, 1999) is
probably the best " what" COM+ book. The book describes t he
services available by COM+ and provides sidebar sum m aries for t he
busy reader . I t is one of the first COM+ books, and Platt worked on

it closely wit h the COM+ t eam .
I also used t he MSDN Library ext ensively, especially the
"Com ponent Services" sect ion, while writing this book. Although the
infor mat ion in this libr ary tends to be t erse, t he overall struct ure is
good. Use this book to learn how to apply COM+ productively and
15
effectively, and use the MSDN Library as a reference for technical
det ails and a source for additional inform ation.


How to Contact Us
We have t est ed and verified the inform ation in t his book to the best
of our ability, but you m ay find t hat features have changed ( or even
that we have made m istakes!) . Please address com m ents and
quest ions concerning this book t o the publisher:
O’Reilly & Associates, I nc.
101 Morris Street
Sebastopol, CA 95472
( 800) 998- 9938 (in the Unit ed St at es or Canada)
( 707) 829- 0515 (international/ local)
( 707) 829- 0104 (fax)
The web site for the book list s exam ples, errata, and plans for
fut ure edit ions. You can access t his page at:
htt p: / / www.oreilly.com / catalog/ com dotnetsvs
To ask technical questions or com ment on this book, send em ail t o:
bookquestions@or eilly.com
Or t o m e directly:
juval.low y@com ponent ware.net
For m ore inform at ion about our books, conferences, software,
resource centers, and the O’Reilly Net work, see our web site:

htt p: / / www.oreilly.com


Acknow ledgm ent s
A book is by no m eans the product of just the author’s work. I t is
the result of m any events and individuals, like links in a chain. I
cannot possibly nam e everyone, ranging from m y parents to m y
friends. I am especially grateful for m y two friends and colleagues,
Marcus Pelletier and Chris W. Rea. Marcus worked wit h m e on large
COM+ projects, and t ogether we confront ed the unknown. Marcus’s
thoroughness and t echnical expertise is a m odel for every
program m er. Chris’s com ments and insight into a reader’s m ind
hav e contributed great ly to t his book’s accuracy, int egrity, and flow.
I w ish to t hank Yasser Shohoud for verifying my approach to
transact ion processing and sharing with m e his own, Richard Grimes
for reviewing the book, and Roger Sessions for writ ing the
Foreword. Thanks also to Johnny Blumenst ock for providing m e with
a place to write. Finally, t his book would not be possible without m y
16
wife, Dana, whose constant support and encouragem ent m ade this
book a realit y. Thank you, Dana.

17
Chapter 1 . COM+ Com ponent Services
By now, m ost developers of large- scale enterprise applications are
convinced of the benefits of com ponent- orient ed developm ent . They
hav e discovered that by break ing a large system down into sm aller
unit s, they can writ e code t hat is easier to reuse on other projects,
easier t o distribute across mult iple com put ers, and easier t o
m aintain. As long as these com ponent s adhere to a binary standard

that defines how t hey com municate wit h one anot her, they can be
invoked as needed at runtim e and discarded when they have
finished their work. This type of application is also particularly
suit ed to the Web, where client s request services of rem ote
applications and then, once satisfied, m ove on to other tasks.
For nearly a decade, the Microsoft Com ponent Obj ect Model (COM)
has been t he standard for com ponent s that run on Windows
m achines, including Windows 9x and Me client s and Windows NT
and 2000 servers. The COM m odel is well docum ented by t he
Microsoft Com ponent Obj ect Model Specification. Tools such as
Visual C+ + and Visual Basic m ake it easy to create COM
com ponents, and scores of books, t raining classes, and art icles are
available to teach program m ers how to use them . Many features of
the Windows operating system are now im plement ed as COM
com ponents, and m any com panies have invested heavily in COM-
based sy stems of t heir own.
I n July 2000, Microsoft announced a radically new component m odel
as part of its .NET developm ent platform , suddenly calling int o
quest ion the viability of existing COM applications. .NET com ponent s
bear lit tle resem blance to legacy COM com ponent s and are not
backwards com patible. They can be m ade to int eroper at e with COM
com ponents but do not do so naturally.
When it com es t o the services and tools program mers use to build
ent erprise- scale .NET applications, however, one facility continues
to provide the necessary runtim e infrastructure and services: COM+
com ponent services. These services have been available on
Windows 2000 since it s release, but they will gain greater
im portance in t he m onths ahead. As it turns out , they offer a bridge
bet ween tradit ional COM and .NET applications, m aking your
understanding and m ast ery of them as im port ant now as it has ever

been.
I n this chapter, we provide a quick over view of the COM+ suite of
com ponent services and t hen introduce you to the Com ponent
Serv ices Explorer, your prim ary tool for building and m anaging both
COM and .NET enterprise applicat ions. You will also creat e, debug,
and deploy a sim ple COM+ "Hello World" application, using a
traditional COM com ponent and learning about COM+ application
types and configured com ponents as you do so.
18


1 .1 COM+ Com ponent Services
Com ponent s need runtim e services to work. The original COM
runt im e supported com ponent s located on t he sam e machine,
typically a desktop PC. As the focus of Windows development
shift ed from standalone PCs to net worked syst em s, Microsoft found
it necessary to add additional services (see The Evolut ion of COM+
Serv ices) . First, t hey added support for distributed applications, or
applications whose com ponents are located on m ore t han one
m achine ( som etim es referred t o as "COM on a wire" ). Later,
Microsoft added new services to support ent er prise applicat ions,
whose com plexity and scale placed new dem ands on t he resources
of a system and required an entirely new level of support . These
trends were only exacerbat ed by the m ove to web-based
applications aim ed at huge num bers of custom ers connect ed over
the public Internet.
Collectively, the services that support COM and .NET com ponent-
based applications are known as the COM+ com ponent services, or
sim ply as COM+ .
The Evolut ion of COM + Services

COM solved a num ber of problem s facing early com ponent
developers by providing a binary st andard for com ponents,
defining a com m unicat ion interface, and providing a way to
link com ponents dynam ically. COM freed developers from
hav ing t o deal with "plum bing" and connectivity issues,
allowing them to concent rate on designing com ponent s.
By t he m id-1990s, however, it was clear that Windows
developers needed additional services to support distribut ed
and transaction-oriented applications. Distributed COM
( DCOM) was released in 1995, a specification and service
used to distribut e com ponents across different m achines and
invoke them rem ot ely. T
hen, Microsoft r eleased the Microsoft
Transact ion Serv er ( MTS) in 1998, which provided
com ponent developers with new services for transaction
m anagem ent, declarative role-based security, instance
activat ion m anagem ent , com ponent deploym ent and
inst allation, and an adm inistration tool for m anaging
com ponent configurations.
There was m ore to MTS than just new services. MTS
represented a program ming m odel in which t he com ponent
developer simply declared ( using t he MTS adm inistrative
tool) which services a com ponent required, and left it to MTS
to provide an appropr iate runt im e environment . Developers
19
could now spend even less effort on low- level service
plum bing ( such as interact ing wit h t ransaction processing
m onitors or m anaging the life cycle of an object ), and m ore
on the business logic the cust om er paid for. Yet, MTS had it s
lim it ations. Forem ost was the fact that MTS was built on top

of conventional COM/ DCOM. The underlying operating
system and COM itself were unaware that MTS even existed.
MTS resorted to esoteric hacks and kludges t o provide its
services, and MTS could not provide it s services to every
COM obj ect ( m ultithreaded apartm ent obj ect s were
excluded). Som e services, such as object pooling, were
either not possible or unavailable.
The developm ent of a new version of the Windows NT
operat ing syst em (init ially called NT 5.0 and later renam ed
Windows 2000) , gave Microsoft an opportunit y to correct t he
deficiencies of MTS and DCOM by fusing t hem into a new
com prehensive component services suit e. Microsoft added
yet m ore services, including obj ect pooling, queued
com ponents, and events, and m ade t he package a part of
the core Windows operating syst em . The new suite of
services was named COM+ 1.0, the subject of this book. The
nex t version of COM+ , COM+ 1.5, is scheduled for release
wit h Windows XP in Q4 2001 and is described in Appendix B.

The COM+ acronym is an overloaded and oft en m isused
term. Today it is used inform ally to refer to bot h the lat est
version of the COM com ponent specification and the
com ponent services available on the lat est versions of
Windows. I n this book, we use the term COM+ to refer t o the
COM+ com ponent services. When we speak of COM+
com ponents, we refer t o COM com ponent s configured t o run
under t hose services. Howev er, as you will see, a COM+
application m ay consist of either COM or .NET com ponents
( see COM+ : The Migration Path to .NET) .
Here is a quick sum m ary of t he most im portant serv ices provided

by COM+ :
Administration
Tools and serv ices that enable program mers and
adm inist rators to configure and m anage com ponents and
com ponent-based applicat ions. The m ost im portant t ool is the
Microsoft Managem ent Console Com ponent Services Explorer.
COM+ also prov ides a st andard locat ion, the COM+ Catalog,
for storing configurat ion inform ation. The Com ponent Services
Explorer is explained in the following sect ion. The COM+
Catalog is described in Chapter 6.
Just -in- Tim e Act ivat ion (JI TA)
20
Serv ices that inst antiat e com ponents when they are called
and discard t hem when their work is done. JITA is explained
in Chapt er 3.
Obj ect pooling
Serv ices that allow instances of frequently used, but
expensive, resources, such as database connect ions, to be
m aintained in a pool for use by num er ous client s. Obj ect
pooling can im prove t he perform ance and responsiveness of a
dist ribut ed applicat ion dram atically. I t is explained in Chapt er
3.
Transact ions
Serv ices that allow oper at ions carr ied out by distributed
com ponents and resources such as databases t o be treated as
a single operat ion. Transaction m anagem ent is a requirem ent
of m ost com mercial system s. COM+ Transact ion services are
discussed in Chapt er 4.
Synchr onization
Serv ices for cont rolling concurrent access to objects. These

services are explained in Chapt er 5.
Securit y
Serv ices for aut hent icating clients and cont rolling access t o an
application. COM+ supports role-based security, which is
explained in Chapter 7.
Queued com ponent s
Serv ices that allow com ponents t o com municate through
asynchronous messaging, a feature t hat m akes possible
loosely coupled applications or even disconnected
applications. Queued com ponent s are discussed in Chapter 8.
Event s
Serv ices that allow com ponents t o inform one anot her of
significant event s, such as changes in data or sy stem state.
COM+ supports a publish-subscribe model of event
notification, which is described in Chapt er 9.
To sum m arize, COM+ is about com ponent services and has alm ost
nothing to do with t he way a com ponent is developed. The .NET
fram ework allows you t o develop binary com ponents more easily
than does COM, but it continues t o rely on com ponent services
available through COM+ . The m anner in which .NET and COM
com ponents are configured t o use these services, however , is not
the sam e. Currently, m ost Windows enterprise developers are
developing applications based on the existing COM standard using
Visual Basic 6 and Visual C+ + 6 with ATL. For t his reason, t his book
uses COM exam ples t o dem onst rate COM+ . However, these sam e
services are available t o .NET com ponents as well. Chapt er 10
shows you how to use them .
COM+ 1.0 is an integral part of Windows 2000 and requires no
special inst allation. Som e COM+ feat ures ar e available only when
21

both the client and server are running on Windows 2000 m achines,
but COM+ clients can usually run on Windows 9.x and Windows NT
m achines as well.
COM + : The Migrat ion Path to .N ET
.NET is Microsoft ’s next-generation com ponent t echnology
and application development platform . ( For a quick overview
of the .NET platform , see Appendix C.) However, adopting a
radically new technology such as .NET is never an easy
endeav or for com panies and developers. Most have m ade a
considerable invest ment in an existing, oft en COM- based,
code base and t he developer skills needed to m aintain it.
Unless com panies have a com pelling reason t o m ove t o .NET
or a reasonable m igration path, they post pone or avoid
m aking t he change.
However, because COM and .NET com ponents can coexist in
the sam e COM+ application, com panies can cont inue t o build
COM com ponents today, adding .NET serviced com ponents to
their applications at a later t im e when the advantages of
doing so are m ore com pelling. This is a migration strat egy
wort h your consideration.
When Windows XP is released in Q4 2001, it will include a new
version of COM+ com ponent services, COM+ 1.5. This new ver sion
im proves COM+ 1.0 usability and addresses som e of the pitfalls of
using COM+ 1.0 on Windows 2000, as described in this book. COM+
1.5 also adds new feat ures to ex ist ing services and lays the
foundation for int egration wit h .NET web services. Appendix B
sum mar izes the fort hcom ing changes.


1 .2 The Com ponent Services Ex plorer

COM+ com ponents and applications are m anaged through the
Com ponent Services Explorer ( form erly known as t he COM+
Explorer) .The Com ponent Services Explorer is a Microsoft
Managem ent Console snap- in and is available on every Windows
2000 m achine.
To fire up t he Component Services Explorer, go t o the Start m enu
and select Set tings Cont rol Panel. When the Control Panel window
appear s, select t he Adm inist rative Tools directory and t hen select
the Com ponent Services application.
The first t hing you should do after locat ing the Component Services
Explorer is create a shortcut to it on your desktop. As a dev eloper,
you need easy access t o the Com ponent Services Explorer, your
m ain gateway into COM+ ( see Figure 1-1) . You can use the
Com ponent Services Explorer to create and configure COM+
22
applications, im port and configure COM or .NET component s, export
and deploy your applications, and adm inist er your local machine.
You can even adm inister COM+ on other m achines on the net work,
provided you have adm inistrative privileges on t hose m achines.
A COM+ applicat ion is a logical group of COM+ com ponents.
Com ponent s usually share an application if they depend on one
anot her to accom plish their tasks and when all the com ponent s
require the sam e application level configuration, as with security or
activat ion policy. Com ponents in t he sam e application are often
developed by the sam e team , and are m eant to be deployed
together.
You can see all the COM+ applications inst alled on your m achine by
opening the Com ponent Ser vices Explorer and expanding the
Com puters folder in the Tree window: Com puters My Com put er
COM+ Applications. Every icon in t he COM+ Applicat ions folder

represents a COM+ application. Each COM+ application contains
COM+ com ponents. Com ponents m ust be explicit ly im port ed into
the Com ponent Services Explorer t o t ake advantage of COM+
services.
The Com ponent Services Explorer offers a hierarchical approach to
m anaging COM+ serv ices and configurations: a com puter contains
applicat ions, and an applicat ion contains com ponents. A com ponent
has interfaces, and an int erface has m ethods. Each it em in the
hierarchy has its own configurable properties. Not e that the
hierarchy allows you to view the param eters of any m ethod listed in
the hierarchy.
Figu re 1 - 1 . Th e Com pon ent Se rvices Ex plor e r



1 .3 H ello COM+
The best way to becom e acquaint ed wit h the Com ponent Services
Explorer and basic COM+ term inology is to do a trivial exam ple.
This sect ion walks you through the COM+ equivalent of the
23
canonical "Hello World" program. You will build a COM+ applicat ion
cont aining a COM com ponent that displays a m essage box saying
"Hello COM+ ".
When developing your "Hello COM+ " application, follow these steps:
1. Create a classic COM com ponent. All COM+ com ponents st art
their life as classic COM com ponents, developed with such
tools as ATL, MFC, or Visual Basic 6.0.
2. Create a new COM+ application to host the com ponent .
3. Add the com ponent to t he application.
4. Writ e a client and test the component.

The rest of this chapt er uses t his "Hello COM+ " example to
dem onstrate various COM+ features and capabilit ies. The exam ple
is also available as part of the source files provided wit h this book
( see the Preface for inform ation on how t o access t hese files) .
1 .3 .1 Building a COM Com ponent
We will use ATL 7.0 t o generate a classic COM com ponent, although
you can also do it in Visual Basic 6.0 wit h alm ost the sam e ease.
Start a new ATL pr oject in Visual Studio.NET and nam e it Hello. For
sim plicity, do not use Attribut ed project ( deselect Att ribut ed in t he
ATL Proj ect Wizard under Application Set tings) . Also, do not select
COM+ 1.0 support. This select ion adds a few interfaces explained in
subsequent chapt ers that are not relevant to t his exam ple. Bring up
the Add Class dialog ATL and select t he Sim ple ATL Obj ect item.
This st ep should bring up t he ATL Sim ple Obj ect Wizard dialog (see
Figure 1-2) . Type the following entr ies, in order:
1. I n the Short Nam e field, enter Message .
2. I n the CoClass field, enter Hello .
Your com pleted dialog should look like Figure 1-2. There is no need
to access the Options selection in the dialog (j ust use t he defaults) .
Click OK when you’re done.
Figu re 1 - 2 . Use the ATL object w iz ard to generate a sim ple COM obj e ct
24

Right- click t he I Message interface icon in the Class View, and select
Add and then Add Method from the pop-up contex t m enu. This
st ep brings up the Add Method Wizard. Enter ShowMessage as the
m ethod nam e and click OK.
Aft er following these st eps, the ATL Object Wizard will generat e a
new interface definition in the proj ect I DL file, and the new m ethod
wizard will add a m et hod to that int er face. Verify that the interface

definit ion in t he I DL file looks like t his:
[
//various IDL attributes
]
interface IMessage : IDispatch
{
[id(1), helpstring("method ShowMessage")] HRESULT
ShowMessage( );
};
Also m ake sure that t he IDL file contains a t ype library section wit h
the CoClass definition:
[
//you will have a different CLSID here:
uuid(C530E78E-9EE4-47D3-86CC-3B4EE39CBD26),
helpstring("Message Class")
]
coclass Hello
{
[default] interface IMessage;
};
Next, go to the m essage.cpp file and im plem ent the ShowMessage( )
m ethod of the CMessage class:
STDMETHODIMP CMessage::ShowMessage( )
{
25
::MessageBox(::GetActiveWindow( ),"Hello COM+","First
COM+ Application",MB_OK);
return S_OK;
}
You can now com pile and build the DLL. Every COM+ com ponent

m ust reside in a DLL, and that DLL m ust cont ain a type library
em bedded in it as a resource. ATL will com pile and build t he DLL for
you and add a reference to the type library in t he proj ect r esource
file, the hello.rc file. COM+ does not require you to regist er your
com ponent, although the ATL build process will regist er it for you.
As you will see later, COM+ m aintains its own com ponents
registrat ion and configuration repository.
1 .3 .2 Crea ting a COM + Applica t ion
Open the Com ponent Services Explorer and expand My Com puter
COM+ Applications folder. Right- click t he COM+ Applications
folder and select New Application from the pop-up cont ext m enu.
This st ep brings up the Application I nst all Wizard. Click Next on the
first wizard screen.
I n the next wizard screen, select t he Create an Em pt y Application
option in t he next wizard screen. Now the wizard will let you specify
the new application nam e and it s application type, which can be
either a library or a server type ( see Figure 1-3). Ent er Hello COM+
for the application nam e, and change the application type from the
default Server application t o Library application. A library
applicat ion indicat es t hat the com ponents in the application will be
loaded direct ly in the process of their client s ( lik e a classic COM in-
proc server) . A ser ver applicat ion indicates that t he com ponent s will
run in their own process ( sim ilar to a classic COM local ser ver) . You
can always change t he application nam e and its activat ion t ype later
wit h the Com ponent Services Explorer . Click Nex t and Finish in t he
last wizard screen. You have just created your first COM+
application.
Figu re 1 -3 . N am ing you r n ew COM+ applicat ion an d con figu rin g it t o be a
library or a ser ver app lica t ion

×