PROFESSIONAL MINDWARE
TM
C# COM+ Programming
CD-ROM includes all
examples and source code
Visit us at mandtbooks.com
C# COM+
Programming
Derek Beyer
“It took technical grace to forge a waltz between today’s COM+
Services and tomorrow’s evolved world of Next Generation
development in C#, and this book is your dancing instructor.”
— Michael Lane Thomas, .NET Series Editor
C# COM+ Programming
C# COM+
Programming
Derek Beyer
M&T Books
An imprint of Hungry Minds, Inc.
Best-Selling Books
●
Digital Downloads
●
e-Books
●
Answer Networks
●
e-Newsletters
●
Branded Web Sites
●
e-Learning
New York, NY
●
Cleveland, OH
●
Indianapolis, IN
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND AUTHOR HAVE USED THEIR BEST
EFFORTS IN PREPARING THIS BOOK. THE PUBLISHER AND AUTHOR MAKE NO REPRESENTATIONS OR
WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS BOOK
AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A
PARTICULAR PURPOSE. THERE ARE NO WARRANTIES WHICH EXTEND BEYOND THE DESCRIPTIONS
CONTAINED IN THIS PARAGRAPH. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES
REPRESENTATIVES OR WRITTEN SALES MATERIALS. THE ACCURACY AND COMPLETENESS OF THE
INFORMATION PROVIDED HEREIN AND THE OPINIONS STATED HEREIN ARE NOT GUARANTEED OR
WARRANTED TO PRODUCE ANY PARTICULAR RESULTS, AND THE ADVICE AND STRATEGIES CONTAINED
HEREIN MAY NOT BE SUITABLE FOR EVERY INDIVIDUAL. NEITHER THE PUBLISHER NOR AUTHOR SHALL
BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHER COMMERCIAL DAMAGES, INCLUDING BUT NOT
LIMITED TO SPECIAL, INCIDENTAL, CONSEQUENTIAL, OR OTHER DAMAGES.
Trademarks: Professional Mindware is a trademark or registered trademark of Hungry Minds, Inc. All other
trademarks are property of their respective owners. Hungry Minds, Inc., is not associated with any product or vendor
mentioned in this book.
is a trademark of
Hungry Minds, Inc.
C# COM+ Programming
Published by
M&T Books
an imprint of Hungry Minds, Inc.
909 Third Avenue
New York, NY 10022
www.hungryminds.com
Copyright © 2001 Hungry Minds, Inc. All rights
reserved. No part of this book, including interior
design, cover design, and icons, may be
reproduced or transmitted in any form, by any
means (electronic, photocopying, recording, or
otherwise) without the prior written permission of
the publisher.
Library of Congress Control Number: 2001089342
ISBN: 0-7645-4835-2
Printed in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/SR/QZ/QR/IN
Distributed in the United States by
Hungry Minds, Inc.
Distributed by CDG Books Canada Inc. for
Canada; by Transworld Publishers Limited in the
United Kingdom; by IDG Norge Books for
Norway; by IDG Sweden Books for Sweden; by
IDG Books Australia Publishing Corporation Pty.
Ltd. for Australia and New Zealand; by
TransQuest Publishers Pte Ltd. for Singapore,
Malaysia, Thailand, Indonesia, and Hong Kong;
by Gotop Information Inc. for Taiwan; by ICG
Muse, Inc. for Japan; by Intersoft for South
Africa; by Eyrolles for France; by International
Thomson Publishing for Germany, Austria, and
Switzerland; by Distribuidora Cuspide for
Argentina; by LR International for Brazil; by
Galileo Libros for Chile; by Ediciones ZETA S.C.R.
Ltda. for Peru; by WS Computer Publishing
Corporation, Inc., for the Philippines; by
Contemporanea de Ediciones for Venezuela; by
Express Computer Distributors for the Caribbean
and West Indies; by Micronesia Media Distributor,
Inc. for Micronesia; by Chips Computadoras S.A.
de C.V. for Mexico; by Editorial Norma de
Panama S.A. for Panama; by American Bookshops
for Finland.
For general information on Hungry Minds’
products and services please contact our Customer
Care department within the U.S. at 800-762-2974,
outside the U.S. at 317-572-3993 or fax
317-572-4002.
For sales inquiries and reseller information,
including discounts, premium and bulk quantity
sales, and foreign-language translations, please
contact our Customer Care department at
800-434-3422, fax 317-572-4002 or write to
Hungry Minds, Inc., Attn: Customer Care
Department, 10475 Crosspoint Boulevard,
Indianapolis, IN 46256.
For information on licensing foreign or domestic
rights, please contact our Sub-Rights Customer
Care department at 212-884-5000.
For information on using Hungry Minds’ products
and services in the classroom or for ordering
examination copies, please contact our
Educational Sales department at 800-434-2086 or
fax 317-572-4005.
For press review copies, author interviews, or
other publicity information, please contact our
Public Relations department at 317-572-3168 or
fax 317-572-4168.
For authorization to photocopy items for
corporate, personal, or educational use, please
contact Copyright Clearance Center, 222
Rosewood Drive, Danvers, MA 01923, or fax
978-750-4470.
is a trademark of
Hungry Minds, Inc.
About the Author
Derek Beyer is currently working as a Web development specialist at Meijer Stores
in Grand Rapids, Michigan. Derek mentors other developers on application design
issues and development techniques. He is also responsible for implementing and
maintaining core infrastructure components such as Web and application servers.
Derek has developed and evangelized development guidelines for corporate devel-
opers in the areas of MTS, COM+, Visual Basic, and Active Server Pages.
Derek has also worked as a consultant for the Chicago-based consulting company
March First. He has been involved with projects ranging from developing applica-
tions for a major Internet-based consumer Web site to Web integration of SAP R/3
applications. Derek also speaks at user group meetings on the topic of COM+
and .NET.
In his free time, Derek can usually be found getting some much-needed exercise
at the gym or enjoying outdoor activities such as hunting and fishing.
About the Series Editor
Michael Lane Thomas is an active development com-
munity and computer industry analyst who presently
spends a great deal of time spreading the gospel of
Microsoft .NET in his current role as a .NET technol-
ogy evangelist for Microsoft. In working with over a
half-dozen publishing companies, Michael has written
numerous technical articles and written or contributed
to almost 20 books on numerous technical topics,
including Visual Basic, Visual C++, and .NET tech-
nologies. He is a prolific supporter of the Microsoft
certification programs, having earned his MCSD,
MCSE+I, MCT, MCP+SB, and MCDBA.
In addition to technical writing, Michael can also be heard over the airwaves from
time to time, including two weekly radio programs on Entercom (http://
www.entercom.com/) stations, including most often in Kansas City on News Radio
980KMBZ ( He can also occasionally be caught on the
Internet doing an MSDN Webcast ( />webcasts/) discussing .NET, the next generation of Web application technologies.
Michael started his journey through the technical ranks back in college at the
University of Kansas, where he earned his stripes and a couple of degrees. After a
brief stint as a technical and business consultant to Tokyo-based Global Online
Japan, he returned to the States to climb the corporate ladder. He has held assorted
roles, including those of IT manager, field engineer, trainer, independent consul-
tant, and even a brief stint as Interim CTO of a successful dot-com, although he
believes his current role as .NET evangelist for Microsoft is the best of the lot. He
can be reached via email at
Credits
ACQUISITIONS EDITOR
Sharon Cox
PROJECT EDITOR
Matthew E. Lusher
TECHNICAL EDITOR
Nick McCollum
COPY EDITOR
C. M. Jones
EDITORIAL MANAGER
Colleen Totz
PROJECT COORDINATOR
Dale White
GRAPHICS AND PRODUCTION
SPECIALISTS
Laurie Stevens
Brian Torwelle
Erin Zeltner
QUALITY CONTROL TECHNICIANS
Carl Pierce
Charles Spencer
PERMISSIONS EDITOR
Carmen Krikorian
MEDIA DEVELOPMENT SPECIALIST
Gregory W. Stephens
MEDIA DEVELOPMENT COORDINATOR
Marisa Pearman
BOOK DESIGNER
Jim Donohue
PROOFREADING AND INDEXING
TECHBOOKS Production Services
COVER IMAGE
© Noma/Images.com
For
Mom and Dad,
without whom none of this would have been possible for so many reasons
Preface
Welcome to C# COM+ Programming. If you have purchased this book or are currently
contemplating this purchase, you may have a number of questions you are hoping
this book will answer. The most common questions I get are “Is COM+ dead?” and
“What is COM+’s role in .NET applications?” The answer to the first question is a
definite “no”! The COM+ technology that Microsoft has included with Windows 2000
is still available to .NET programmers. In fact, some COM+ technologies that were
previously available only to C++ programmers can now be used by Visual Basic .NET
and C# programmers. The second question is always a little harder to answer. The
typical response you would get from me is “it depends.” The technologies found in
COM+ such as distributed transactions and queued components can be found only in
COM+. The question to ask yourself when trying to decide if you should use a partic-
ular COM+ service is “Do I need this service in my application?” If the answer is yes,
then feel free to use COM+. If the answer is no, then COM+ is not a good fit for your
application.
All of the code examples used in the book use the new programming language
C#. C# is an object-oriented programming language developed specifically for
.NET. In fact, .NET applications are the only applications you can write with C#.
Throughout the book I point out the language features of C# that can help you
write better COM+ components. Although all of the code is in C#, the examples can
also be rewritten in C++ if you like.
Whom This Book Is For
COM+ is not a topic for novice programmers. If you have never developed an appli-
cation before, then this book probably is not for you. When talking about COM+, the
conversation invariably goes toward distributed computing. If you have developed
applications, particularly distributed Web applications, then the topics covered in this
book will make much more sense to you.
If you are new to .NET programming or COM+ programming, do not fear. Part I
of this book covers the basics of .NET and interacting with COM components. Part I
provides you with the grounding you will need to understand how .NET applications
work and how they interact with legacy COM components. If you are new to .NET
programming, I strongly suggest you read Chapter 1 before reading any of the other
chapters. Chapter 1 introduces you to the .NET environment. If you don’t understand
how the environment works, the rest of the book will not make much sense to you.
For those of you new to C#, Appendix C provides you with an introduction to
the language. Appendix C covers the basic features of the language such as data
types, loops, and flow control statements as well as the specific language features
used in the rest of the book.
ix
This book assumes that you are not familiar with COM+ programming. Each
chapter covers the basics features and issues about each COM+ service. You do not
have to be an experienced COM+ developer to learn how to develop COM+ compo-
nents with this book.
How This Book Is Organized
This book is divided into three parts. Each part provides information that you will
need to understand the following part. The parts of this book provide a logical pro-
gression that you will need in order to build your skills and understanding of COM+
programming in .NET.
Part I: Interoperating with COM
Part I covers the basics of the .NET runtime environment called the Common
Language Runtime. Because every .NET application runs in the Common Language
Runtime, it is crucial that you understand this environment if you are to develop
COM+ components with C#. The bulk of Part I covers interoperating with the COM
world. I show you how to consume legacy COM components from C# applications.
I also show you how to write C# components that COM clients can consume. An
understanding of COM interoperation with .NET is important if you develop dis-
tributed applications that use COM components or are used from COM components.
Part II: COM+ Core Services
Part II covers the core services of COM+. All of the typical services such as distributed
transactions, role-based security, loosely coupled events, and queued components,
among others, are covered in Part II. The chapters in this part are organized (as best
as possible) from the more easy services to more advance services.
Part III: Advanced COM+ Computing
The final part of this book, Part III, covers some of the more advanced topics of
COM+. Part III covers the .NET remoting framework. The .NET remoting framework
provides a developer with a way to call methods of a component from across the
network. As you will see, COM+ components written with C# can plug into
the remoting framework by virtue of their class hierarchy. Part III also discusses the
new features of COM+, Internet Information Server and Microsoft Message Queue
(all of these technologies are used in the book) currently slated for Windows XP.
Many of the new features of COM+ center on providing a more stable environment
for COM+ components.
x Preface
Conventions Used in This Book
Every book uses some several conventions to help the reader understand the material
better. This book is no exception. In this book I used typographical and coding con-
ventions to help make the material more clear.
Typographical Conventions
Because this is a programming book, I have included lots of code examples. I cover
each code example (the larger ones have their own listing numbers) almost line for
line. Paragraphs that explain a particular code example often refer to the code from
the example. When I refer to code from the example, it is always in monospaced
font. Here is an example from Chapter 5.
using System;
using Microsoft.ComServices;
[assembly: ApplicationAccessControl(
AccessChecksLevel = AccessChecksLevelOption.ApplicationComponent
)
]
public class SecuredComponent {
// some method implementations
}
Notice that I use the assembly keyword inside the attribute tags. This tells the C#
compiler that the attribute is an assembly-level attribute. Inside the attribute decla-
ration, I have set the AccessChecksLevel property to application and component
by using the AccessChecksLevelOption enumeration.
The code example above (the line starting with using System;) is set entirely in
monospaced font. The paragraph above explains the code example. In this paragraph
I refer to keywords from the code example such as assembly, AccessChecksLevel,
and AccessChecksLevelOption. Wherever you see something in monospaced font
inside a paragraph, there is a good chance that it is a keyword that was used in a
previous or forthcoming code example.
Coding Conventions
The .NET framework uses Pascal casing to name most of its classes, method para-
meters, enumerations, and so on. The code examples used in this book follow this
practice. Pascal casing capitalizes the first letter of each word in a name. For exam-
ple, if I wrote a class that accessed customer order information, I might name it
CustomerOrders. Because I use Pascal casing, I must capitalize the C of Customer
and the O of Orders. I use this convention to help make the code examples more
readable.
Preface xi
Icons Used in This Book
Many of the topics covered in this book have related topics. Quite often it is impor-
tant for you to understand these related topics if you are to understand the central
topic being discussed. It is can be rather easy however, to lose a reader if you go too
far off on a tangent. In order to both cover the important information and not lose
you, the reader, I’ve put these topics into a Note. For example:
Notes explain a related topic. They are also used to remind you of particular
features of C# that can help you write good COM+ components.
xii Preface
Acknowledgments
I am truly grateful to the team of reviewers and editors who worked so hard and dili-
gently on this book. Although my name appears on the cover, this book is truly a
team effort. Matt Lusher and Eric Newman filled the project editor role on this project
and provided great feedback. Matt made stressful times much more bearable through
his professionalism and good humor. Chris Jones caught the grammar mistakes
I made late at night while I was sleepy and bleary-eyed. A good acquisitions editor
glues the whole book together and tries to keep everyone happy, and Sharon Cox was
terrific in this capacity. Sharon no doubt buffered me from lots of issues that I would
normally have had to deal with. Thank you, Sharon! I owe a huge debt of gratitude to
the Production Department at Hungry Minds; these folks are the ones who suffered
my artwork and screenshot mistakes. You guys really came through in a pinch.
I should also thank Rolf Crozier, who was the acquisitions editor early on in this
book. Rolf pitched the book idea to Hungry Minds and got the whole ball rolling.
The best part about being in a field that you love is the people you get to share
your ideas with and learn from. Steve Schofield is the most enthusiastic guy I have
ever met when it comes to learning new technology. His excitement for .NET is
infectious. Steve also provided me with the contacts inside Hungry Minds I needed
to make this book a reality. Nick McCollum was an awesome technical editor for the
book. He kept me honest throughout and helped me relate many topics better to the
reader. I would also like to thank a couple of key Microsoft employees, Mike
Swanson and Shannon Paul. Mike was always there to offer assistance and get
things I needed. He also absorbed many of my complaints about the technology
with a smile and a nod. Shannon provided me with key information about COM+
events. He also kept me on track when it came to that subject. Thank you, Shannon.
I now realize that writing a book is a monumental undertaking. No one can
undertake such an endeavor without the proper support system of friends and fam-
ily. I am fortunate enough to have a wonderful support system. The cornerstone of
that system are my parents. My dad showed me by example what a work ethic
really is. This is the hardest-working man I have ever seen. I am grateful that some
of his work ethic rubbed off on me. My mother provides me with unconditional
support and encouragement. I must thank her for understanding why she hardly
saw me for months while I was cooped up writing this book. Last but certainly not
least I must thank Jacque. Jacque is a very special friend who bore the brunt of my
crankiness during the course of this book. She was able to pick me up at my lowest
times with her compassion and positive energy. Thank you, sweetie!
Contents at a Glance
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments. . . . . . . . . . . . . . . . . . . . . . . . . xiii
Part I Interoperating with COM
Chapter 1 Understanding .NET Architecture . . . . . . . . . . . . . . 3
Chapter 2 Consuming COM Components from .NET . . . . . . . . 21
Chapter 3 Consuming .NET Components from COM . . . . . . . . 33
Part II COM+ Core Services
Chapter 4 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Chapter 5 Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Chapter 6 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Chapter 7 Object Pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
Chapter 8 Queued Components . . . . . . . . . . . . . . . . . . . . . . 121
Part III Advanced COM+ Computing
Chapter 9 Remoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Chapter 10 The Future of COM+ and .NET . . . . . . . . . . . . . . . 185
Appendix A: What’s on the CD-ROM? . . . . . . . . . 209
Appendix B: The COM+ Shared Property Manager 215
Appendix C: Introduction to C# . . . . . . . . . . . . . . 233
Appendix D: Compensating Resource Managers . . 259
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273