Developing ASP Components, Second Editionby Shelley Powers Copyright © 2001, 1999 O’Reilly & Associates, Inc.. lan-The book also provides a comprehensive overview of the ASP component ro
Trang 1for all ROR Fly Downsky epubcn
Trang 2Developing ASP Components
Trang 4Developing ASP Components
Second Edition
Shelley Powers
Beijing• Cambridge• Farnham• Köln• Paris• Sebastopol• Taipei• Tokyo
Trang 5Developing ASP Components, Second Edition
by Shelley Powers
Copyright © 2001, 1999 O’Reilly & Associates, Inc All rights reserved.
Printed in the United States of America.
Published by O’Reilly & Associates, Inc., 101 Morris Street, Sebastopol, CA 95472.
Editor: Ron Petrusha
Production Editor: Leanne Clarke Soylemez
Cover Designer: Hanna Dyer
Printing History:
April 1999: First Edition.
March 2001: Second Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly & Associates, Inc ActiveX, JScript, Microsoft, MSDN, Visual Basic, Visual C++, Win32, Windows, and Windows NT are registered trademarks and Active Directory is a trademark of Microsoft Corporation Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks Where those designations appear in this book, and O’Reilly & Associates, Inc was aware of a trademark claim, the designations have been printed in caps or initial caps The association between the image of an asp and developing ASP components is a trademark of O’Reilly & Associates, Inc.
While every precaution has been taken in the preparation of this book, the publisher assumes
no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein.
ISBN: 1-56592-750-8
[M]
Trang 6About the Author
Shelley Powersis a consultant/author with her own company, Burning Bird prises In the last several years, she has worked with a variety of distributed,Internet, and Web development applications, for different platforms and using avariety of tools Shelley has authored or coauthored books on Dynamic HTML,JavaScript, Java, CGI, Perl, P2P, general Web technologies, and more Shelley can
Enter-be reached at shelleyp@yasd.com, and her book support site can Enter-be found at
http://www.burningbirdenterprises.com.
Colophon
Our look is the result of reader comments, our own experimentation, and back from distribution channels Distinctive covers complement our distinctiveapproach to technical topics, breathing personality and life into potentially drysubjects
feed-The animal on the cover of Developing ASP Components, Second Edition, is an asp,
which is a term applied to various venomous snakes, including the depicted asp
viper (Vipera aspis) of Europe as well as the Egyptian cobra (Naja haje), thought
to have been the means of Cleopatra’s suicide
Needing to eat at least 50-60% of their body weight in food per week, Europeanasp vipers hunt by lying in wait for approaching prey After grabbing and biting asmall rodent or other prey, they release it and wait several minutes for it to stopmoving; the generally sluggish viper rarely chases prey Vipers know their hometerritory very well, which allows quick escape from their asp-kicking naturalenemies, serpent eagles, and hedgehogs This trick hasn’t helped them escapefrom their greatest threat, the expansion of human civilization, which frequentlywipes out large sections of their territory
The chemical composition of asp viper venom can vary from one population tothe next, hampering initial antivenin development until 1896, but few viper bitefatalities occur in Europe today
Leanne Soylemez was the production editor and proofreader for Developing ASP
Components, Second Edition Norma Emory was the copyeditor, Mary Anne Weeks
Mayo and Colleen Gorman provided quality control, and John Bickelhaupt wrotethe index
Hanna Dyer designed the cover of this book, based on a series design by EdieFreedman The cover image is a 19th-century engraving from the Dover Pictorial
Trang 7Archive Emma Colby produced the cover layout with QuarkXPress 4.1 usingAdobe’s ITC Garamond font.
David Futato designed the interior layout based on a series design by Nancy Priest.Judy Hoer converted the files from MSWord to FrameMaker 5.5 using tools created
by Mike Sierra The text and heading fonts are ITC Garamond Light and mond Book; the code font is Constant Willison The illustrations that appear in thebook were produced by Robert Romano using Macromedia FreeHand 8 andAdobe Photoshop 5 This colophon was written by Nancy Wolfe Kotary
Gara-Whenever possible, our books use a durable and flexible lay-flat binding If thepage count exceeds this binding’s limit, perfect binding is used
Trang 8Oracle 8i Internal Services for Waits, Latches, Locks, and Memory, eMatter Edition
Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
Table of Contents
Preface xi
1 Writing ASP Components 1
The Role ASP Components Play 2
Why Use ASP Components? 3
COM+ Services and ASP Components 5
Accessing Data with ASP Components 8
Windows 2000 Technologies Accessible from ASP Components 10
A Rose by Any Other Name: Programming Language Choice 11
What About ASP.NET? 13
2 Setting Up the ASP Development Environment 14
Configuring the IIS Environment 15
Remote Administration of IIS 27
Using ADSI to Administer IIS Programmatically 29
IIS Admin Object Overview 46
The IIS Base Admin Objects 59
3 ASP Components and COM 69
Overview of COM for ASP Component Developers 70
How COM Is Implemented 76
Notable COM Interfaces 80
COM Datatypes 87
4 ASP Components, Threads, and Contexts 91
What Are Threads? 92
Threads and Apartments 95
Trang 9vi Table of Contents
Oracle 8i Internal Services for Waits, Latches, Locks, and Memory, eMatter Edition
Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
The Threading Models 96
What Are COM+ Contexts? 110
5 COM+ Services and ASP Components and Applications 114
Developing Component-Based Systems 114
The COM+ Interfaces 116
Porting MTS Packages to COM+ Applications 124
Activating COM+ Applications 125
COM+ Services 127
6 ASP Interaction: Scripting and ASP Components 136
Scripting Language Support Within ASP 136
Instantiating Components 139
COM+ Datatypes and Script/Component Interaction 145
Passing Arrays as Parameters 159
Error Handling Between Component and Script 174
7 Creating a Simple Visual Basic ASP Component 180
Creating an In-Process or Out-Of-Process Component 181
Component Instancing 182
Component Execution and Threads 184
Creating an ASP Project 187
Creating Component Methods 188
Generating, Registering, Installing, and Testing the Component 190
Adding Support for COM+ Services 191
Converting MTS Components for Use with COM+ 201
Accessing the ASP Built-in Objects 202
Error Handling 220
Debugging 222
Performance Issues 222
8 Creating ASP/ADO Components 225
Accessing ADO from a VB Component 226
Creating a Simple ADO Component 227
The ADO Model 233
File and Directory Access with ADO Streams and the Record Object 256
Persisting Data 262
9 Creating an ASP Middle Tier with ADO 266
How Separate Should the Layers Be? 267
Creating ADO Data Wrappers 268
Trang 10Table of Contents vii
Oracle 8i Internal Services for Waits, Latches, Locks, and Memory, eMatter Edition
Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
Defining Business Objects 281
Other Data Integrity Issues 295
10 Server-Side XML Through VB ASP Components 298
XML Basics 298
Formatting XML 302
Working with XML in ASP Applications 307
XML and ADO: Saving Recordsets as XML 315
11 Take a Message: Accessing CDO from ASP Components 321
A Brief Overview of CDO 322
The CDO Object Model 322
Send This URL to a Friend 324
Working with the Message Body 328
Retrieving and Reading Messages 338
12 Working with Active Directory from ASP Applications 344
A Brief Overview of Active Directory in Windows 2000 345
Setting Up an Isolated Active Directory Environment 345
A Refresher on ADSI 347
Binding to Active Directory Objects 349
Using the Active Directory Services Viewer 355
Manipulating Containers 357
Searching Active Directory with ADO 362
ASP Example: Add and Manage Users Through the Web 366
13 Working with MSMQ Components 380
MSMQ/ASP Basics 380
Working with Queues 382
Working with MSMQ Messages 389
Using Transactions 394
Journaling 407
A Brief Word on Message Security 412
14 Creating C++ ASP Components 414
ATL or MFC 415
Using ATL AppWizard to Generate the Basic ASP Component Project 416
Adding an ATL Object 421
Code Changes Based on Adding a New Object 426
Adding Methods to the Interface 427
Adding Support for COM+ Services 432
Trang 11viii Table of Contents
Oracle 8i Internal Services for Waits, Latches, Locks, and Memory, eMatter Edition
Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
Converting MTS Components for Use with COM+ 440
Accessing the ASP Built-in Objects 444
Error Handling 463
15 Adding Data Access to C++ Components with ADO 468
ADO Access in Visual C++ 468
The ADO Object Model 469
The Connection Object 470
The Recordset Object 479
The IADORecordBinding Interface 486
The Command Object 490
Stream and Record Objects 497
16 The CDO Interfaces from C++ Components 506
Accessing CDO Interfaces in C++ 506
Creating and Sending a Message 509
Retrieving and Reading Messages 521
17 Accessing Active Directory from C++ Components 528
Binding to Active Directory Objects 529
ADSI Helper Functions 536
Filtering Collections 538
Creating and Removing Active Directory Objects Using ADSI 540
Searching Active Directory with IDirectorySearch 544
18 Accessing MSMQ from C++ ASP Components 550
Adding Support for MSMQ to the C++ Project 550
Working with Queues 553
Searching for a Specific Queue 560
Working with MSMQ Messages 568
Using Transactions 574
19 Persistence with ASP Components Using ATL and MFC 581
Combining MFC and ATL 581
File Access from ASP Components 584
Creating a Serializable Class 588
Persistence Through Object Serialization 591
20 ASP Components Created with Java 596
Creating Java Components 597
Invoking a COM Object in a Java Component 606
Trang 12Table of Contents ix
Oracle 8i Internal Services for Waits, Latches, Locks, and Memory, eMatter Edition
Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
Working with COM+ Services 611
The ASP Built-in Object and Helper Interfaces 619
Accessing ADO from Java Components 633
21 Creating ASP Components with Delphi 642
Using the COM Wizards to Create ASP Components 642
Manually Adding Support for COM+/ASP 653
Working with the ASP Objects 660
Working with ADO 668
Working with Windows 2000 Functionality: CDO 672
22 Perl-Based Components Using ActiveState’s PDK 677
Setting Up PDK 678
Building a Basic Perl Component 679
Accessing the ASP Built-in Objects 689
Working with Data 709
Working with the Win2K Environment 716
23 Creating Scripting Language Components 719
The Windows Script Components Architecture 719
Elements of a WSC File 722
Script Components and ADO 733
The WSC Wizard 736
Creating Script Components with JScript 741
Accessing Windows 2000 Functionality 744
A ASP Built-in Object Quick Reference 751
B The Weaver Database 776
Index 791
Trang 14ASP components are nothing more than specific types of COM-based nents; that is, they’re COM components designed to interface with Active ServerPages and, most commonly, with Microsoft’s Internet Information Server (IIS).Consequently, you need to develop a certain level of familiarity with COM, theComponent Object Model that underlies much of Microsoft’s technology Becom-ing familiar with COM development in turn requires that you become familiar withthreads, so that you can understand how a COM component interacts with a cli-ent as well as the performance issues involved with clients and components thatare based on different threading models.
Once you’ve become familiar with working with a tool that supports COM nent development and you’re aware of some of the issues involved with COMdevelopment, you still have other new technologies to learn As you develop ASPcomponents, you need to become familiar with web-based development in gen-eral and with the ASP environment in particular The way in which your compo-nents interact with the “world” they find themselves in—with the web server, thebrowser, or the web page reader—occurs through built-in objects that Microsofthas provided for ASP development
compo-Originally, the built-in objects could only be instantiated based on specific eventhandlers In IIS 4.0, however, the built-in objects could be accessed from MicrosoftTransaction Server (MTS) objects And now, in IIS 5.0, the ASP built-in objects can
be accessed from COM+ objects In addition, COM+ Services provides a number of
Trang 15pool-to need pool-to become familiar with COM+ as well.
This seems like a very formidable list of tools and technologies, and it is Butwe’re not finished yet Most applications—including more and more ASP applica-tions—require some form of data access If you need to provide support for dataaccess, then you need to become familiar with ActiveX Data Objects (ADO), thedata access technology from Microsoft that’s built on top of OLE DB Frequently,the content of an ASP page is assembled from data found in a message store, orconversely the data gathered from the user’s interaction with an ASP page is sent
in an email or placed in a message store For applications such as these, you need
to become familiar with Collaborative Data Objects for Windows 2000 (CDO).Under Windows 2000 and IIS, a good deal of system information is stored inActive Directory; to retrieve information from and write information to ActiveDirectory, you should know the Active Directory Service Interface (ADSI) Finally,ASP applications, and particularly ASP e-commerce applications, often requirecommunication across systems and involve events that can occur at different times(as, for example, when a user orders products online and a pick list is needed by
a system in the warehouse for printing) To take advantage of such loosely pled events, you should be familiar with Microsoft Message Queue (MSMQ).Finally, once you know the programming language used for the component, thetool used to build the component, the implications of developing a COM-basedcomponent, the functionality available through built-in and COM+–suppliedobjects, and how you can access data and the other services needed by yourapplication, then and only then you can take on the functionality that your com-ponent needs to provide Then, you add additional functionality such as file inputand output, object serialization, access to other Windows functionality, and so on
cou-So, do you feel tired before you even start? Well, I want to tell you that ing ASP components really isn’t all that bad, and in fact, you are about to start hav-ing some fun Not only that, you are also going to learn to work with technologythat faces directly on that road racing to the future: the road to distributed andcomponent-based development
develop-This book introduces you to working with COM development as well as workingwith threads and those pesky little “not threads, not processes”—apartments Italso provides an overview of the ASP operating environment as well as somethings you need to know about COM+ and how to work with it Finally, to com-plete this environment overview, the book explores the interaction between thecomponent and the script used to instantiate and invoke the methods of that com-ponent
Trang 16Who This Book Is For
This book is geared to the developer who has worked with one of the target guages/tools but either has not created COM objects before or has not workedwith developing ASP components or ASP applications I hope that the book pro-vides enough of an introduction to COM and threads to make you feel more com-fortable with these topics if you haven’t worked with them before and to provide agood review if you have The book does not provide an exhaustive overview ofCOM+ and developing COM+ components but does provide, again, enough of anoverview so you feel comfortable working as a developer in a COM+ environ-ment
lan-The book also provides a comprehensive overview of the ASP component ronment, including using tools and wizards in each language/tool to assist in creat-ing the components, and covering every aspect of accessing the built-in ASPcomponents essential for your development effort In addition, the book also pro-vides good coverage of data access using ADO, messaging using CDO for Win-dows 2000, and message queuing using MSMQ
envi-How This Book Is Structured
Informally, this book is divided into four parts The first part introduces ASP ponent development and covers topics that are of concern to all component devel-opers, regardless of the language they use This part consists of six chapters
com-Chapter 1, Writing ASP Components, examines some of the reasons that you'd
Trang 17components Chapter 2, Setting Up the ASP Development Environment, examines
how to set up your development environment to insure that you can develop withmaximum productivity and that your testing doesn't impact on a production sys-tem In addition, the chapter covers programmatic administration of IIS using
Active Directory and the IIS Admin Objects Chapter 3, ASP Components and COM,
examines Microsoft's Component Object Model (COM), which provides the basis
for developing all types of components Chapter 4, ASP Components, Threads, and
Contexts, provides developers with the information that they need to know about
threading models when developing ASP components, and particularly whenaccessing global data from the ASP Application object It also examines the notion
of context (a grouping of objects that share the same requirements), an standing of which is essential to working successfully with COM+ Chapter 5,
under-COM+ Services and ASP Components and Applications, examines the new
inter-faces supported by COM+, shows how components written to take advantage ofCOM+'s predecessor, Microsoft Transaction Server (MTS), can be ported to COM+,
and examines range of services provided by COM+ Chapter 6, ASP Interaction:
Scripting and ASP Components, covers an often-neglected component ment topic: your component may be accessed by any of a number of scripting lan-guages—VBScript, JScript, PerlScript, Python, Rexx, etc.—and communicationbetween script and component is often not as seamless as you'd like The chapterlooks at what you can do when developing your ASP component to insure that itcan work with as many scripting languages as possible
develop-The second portion of the book, which consists of seven chapters, focuses oncomponent development using Visual Basic In addition, its chapters serve as akind of model for how to develop ASP components if you're using a high-levellanguage like Visual Basic that masks much of the complexity of COM and COM+
Chapter 7, Creating a Simple Visual Basic ASP Component, introduces Visual Basic
as a tool for ASP component development and examines how to access the ASP
object model from Visual Basic Chapter 8, Creating ASP/ADO Components, looks
at accessing data in heterogeneous sources using ActiveX Data Objects (ADO)
Chapter 9, Creating an ASP Middle Tier with ADO, discusses component design for
multi-tier applications, focusing particularly on the degree of separation betweenthe middle tier and the client tier The remaining chapters focus on individualtechnologies that developers frequently use when creating ASP Components.These include the following:
• XML is discussed in Chapter 10, Server-Side XML Through VB ASP Components.
• Collaborative Data Objects (CDO) for Windows 2000 is covered in Chapter 11,
Take a Message: Accessing CDO from ASP Components.
Trang 18Preface xv
This is the Title of the Book, eMatter Edition Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
• Active Directory is discussed in Chapter 12, Working with Active Directory
from ASP Applications.
• Microsoft Message Queue (MSMQ) is examined in Chapter 13, Working with
MSMQ Components.
The third portion of the book, consisting of six chapters, treats component opment using Visual C++ In addition, its chapters serve as a kind of model forASP component development using a high-level language like Visual C++ that
devel-exposes much of the complexity of COM and COM+ Chapter 14, Creating C++
ASP Components, introduces Visual C++ as a tool for ASP component
develop-ment and examines how to access the ASP intrinsic objects from a Visual C++
component Chapter 15, Adding Data Access to C++ Components with ADO,
exam-ines accessing data in heterogeneous sources using ADO The next three chapterscover the following individual technologies that are often used in developing com-ponents for IIS 5.0:
• Collaborative Data Objects (CDO) for Windows 2000 is covered in Chapter 16,
The CDO Interfaces from C++ Components.
• Active Directory is discussed in Chapter 17, Accessing Active Directory from
C++ Components.
• Microsoft Message Queue (MSMQ) is examined in Chapter 18, Accessing
MSMQ from C++ ASP Components.
Finally, coverage of Visual C++ and ASP component development ends with
Chapter 19, Persistence with ASP Components Using ATL and MFC, which
dis-cusses ways in which your component can save its data to the filesystem
The final portion of this book features individual chapters on component ment using the following programming languages and environments:
develop-• Java is covered in Chapter 20, ASP Components Created with Java.
• Delphi is discussed in Chapter 21, Creating ASP Components with Delphi.
• Perl is covered in Chapter 22, Perl-Based Components Using ActiveState’s PDK.
• Windows Script Components (WSC), a scriptable yet powerful development
environment for creating ASP components, is discussed in Chapter 23,
Creat-ing ScriptCreat-ing Language Components.
Finally, the book includes two appendixes Appendix A, ASP Built-in Object Quick
Reference, provides a handy guide to the objects, properties, methods, and events
of the ASP object model Appendix B, The Weaver Database, examines the tables
contained in the sample Weaver database, which is used in the book's examples
It can be downloaded from http://vb.oreilly.com.
Trang 19xvi Preface
This is the Title of the Book, eMatter Edition Copyright © 2001 O’Reilly & Associates, Inc All rights reserved.
Obtaining the Sample Code
All of the example source code from Developing ASP Components, Second Edition,
along with the sample Weaver database discussed in Appendix A, is freely
down-loadable from the O’Reilly & Associates web site at http://vb.oreilly.com Just low the link to the book’s title page, then click on the Examples link.
fol-Conventions Used in This Book
Throughout this book, we have used the following typographic conventions:
Italic
Represents intrinsic and application-defined functions, the names of systemelements such as directories and files, and Internet resources such as web doc-uments New terms are also italicized when they are first introduced
Constant width
Indicates a language construct such as a language statement, a constant, or anexpression Interface names appear in constant width Lines of code alsoappear in constant width, as do function and method prototypes
Constant width italic
Indicates replaceable parameter names in prototypes or command syntax andindicates variable and parameter names in body text
Indicates a note or tip.
Indicates a warning.
Comments and Questions
Please address comments and questions concerning this book to the publisher:O’Reilly & Associates, Inc
101 Morris Street
Sebastopol, CA 95472
Trang 20Acknowledgments
I want to thank the book’s tech reviewers, Daniel Creeron and Matt Childs, fortheir thorough reviews and helpful comments I also want to thank Bob Herbst-man and Tatiana Diaz, members of the O’Reilly editorial staff, for their hard workand dedication to this project
I also want to thank my long-suffering editor, Ron Petrusha This is the second tion of this particular book, and he’s done a terrific job of editing both of them Ialso want to thank my coworkers at Skyfish.com for being a terrific group of peo-ple Specifically, I want to thank a certain group of Australians in the company—guys, the best to you all, and may your dreams find you
edi-Finally, thanks to my readers—I’m here because you’re here