Title: Hitchhiker's Guide to COM Part 2
1Hitchhikers Guide to COM (Part 2)DEVQ200-03
Level 300NameTitleDepartmentMicrosoft
Corporation
2Session Prerequisites
- Windows DNA Application Knowledge
- Technologies
- COM, Microsoft Transaction Server (MTS)
- Visual Studio and Visual Basic
- Hitchhiker's Guide to COM (Part 1)
- Introductory knowledge of COM
3Agenda
- COM Context
- Advanced COM Services Concepts
- Compensating Resource Manager (CRM)
- Object Pooling
- Events
- Security
4COM Context Recap
- A Group of Objects in a Process
- Used to Group Compatible Objects
- All objects in a single context have compatible
attributes - Used to Separate Incompatible Objects
- Objects with at least one attribute
incompatibility must live in different contexts - Each Object Belongs to Exactly One Context
- A Process can Contain Multiple Contexts
5COM ContextsRelation to MTS Context Wrappers
- COM Context Replace MTS Context Wrapper
- Context Wrappers No Longer Exist
- Their purpose is now an integrated feature of
COM - No Need to Use SafeRef
- In MTS, SafeRef was used to obtain an object
reference safe to pass outside of its context
wrapper - In COM, simply use normal object references (Me)
6COM Context Where Does Context Come From?
- Objects are Associated with Context at
Activation-Time - COM Compares the Target Classs Requirements to
the Current Contexts Properties - If compatible, object lives in creators context
- If not compatible, object is instantiated in a
compatible context - Non-Configured Classes are Always Activated in
Creators Context
7COM ContextHow Does Context Flow?
- Properties Can Flow to Downstream Objects
- Call and Object Properties Flow Differently
- Call context flows as one object calls to another
object - Object context flows as new contexts are created
- Object context will flow to subsequent compatible
objects - CreateObject Did Not Flow Context Prior to COM
8COM ContextCall Serialization
- Classes Can Be Configured to Use System-Provided
Call Serialization - Uses the Synchronization attribute
- COM Can Put Contexts into a Synchronization
Boundary Called an Activity - An activity is a group of contexts in which no
concurrent execution is desired - Each Context Belongs To At Most One Activity
9COM ContextThe Synchronization Attribute
- Synchronization Has the Following Properties
- Allows one caller to enter the component at a
time - Flows from component to component within a
process - Allows reentrancy from the same caller
10COM ContextJITA and AutoComplete / AutoAbort
- MTS Intertwined Transaction Voting and Doneness
- Using SetComplete SetAbort Methods
- COM Provides Independent Setter/Getters for
Happy and Done Bits Via IContextState
Interface - COM Allows the Done Bit to be Set
Declaratively on a Method-by-Method Basis
11COM ContextJITA and AutoComplete / AutoAbort
Interception Layer
Object Context
12Agenda
- COM Context
- Advanced COM Services Concepts
- Compensating Resource Manager (CRM)
- Object Pooling
- Events
- Security
13CRMWhat is a Resource Manager?
- System Service that Manages Durable Data
- Maintains durable state of an application
- Works with the Transaction Manager
- To provide the application with a guarantee of
atomicity and isolation - SQL Server is an Example of a Resource Manager
14CRMNot a Resource Dispenser
- Resources Dispensers are different
- Services that provides for the synchronization
and management of non-durable resources within a
process - Example, the ODBC resource dispenser manages
pools of database connections
15CRMWhat is a CRM?
- A CRM is a Type of Resource Manager
- Has Transactional ACID Properties
- Atomicity
- Consistency
- Durability
- Does Not Automatically Provide Isolation
- An Easier Way to Build a Resource Manager
16CRMWhat Does It Do?
- Extends Transactional Capabilities to
Non-Transactional Resources - Allows Custom Roll-Back Logic to be Provided
17CRMEasy to Build a Resource Manager
- Developed as a Pair of COM Components
- CRM Worker
- CRM Compensator
- COM Provides
- Transaction Outcome Notifications
- Logging
18CRMWho Is It For?
- CRM Developers
- Develop a specific type of CRM for a specific
task - COM Application Developers
- Use a CRM in their application
- COM Administrators
- Deploy CRM components as part of an application
- COM Application End Users
- Might not even know they are using a CRM
19CRMScenarios for CRM
- RegDB CRM
- Used by COM admin System Application
- Legacy Applications
- Integration
- File System, Registry, Mail
- Beware isolation issues
20CRMHow Does It Work?
- Compensators Implement a Simplified Version of
the DTC 2PC Interfaces - Phase One
- Compensator is asked to prepare
- Phase Two
- Compensator notified of outcome
- System Manages a Durable Log to Store State
Between Phases
21CRMArchitecture
YourCRM.DLL
CRM Infrastructure
CoCreateInstance
CoCreateInstance
Recovery/2PC Infrastructure
22CRMCRM Lifetime
- Logging is Performed in a Write-Ahead Manner
- Log the action before performing it
- Cleanup is Performed the Opposite Way
- Cleanup the action then write log record to
indicate compensation is done - Either Can Occur More Than Once
- If we crash between logging and execution or lose
messages through RPC - CRM Architecture Ensures That Each Operation is
Performed at Least Once
23CRMUsing a Compensator
- Inside the Transaction
- Create the clerk
- For Operations That Require Compensation
- Use clerk to write user-defined log records
- Log Writes are Buffered until ForceLog is Called
- Can Erase Last Log Entry Using ForgetLogEntry
Demo
24CRMRepeatable Compensating Logic
- Use Logic That Can Safely Be Performed More Than
Once - Set bank account balance to 1000
- Not
- Add 100 to bank account
- Logic Can Be Made Repeatable Through Careful
Logging - Add 100 to bank account becomes balance
before transaction is 900, after transaction is
1000. If balance is 900 during recovery,
change may be reapplied.
25CRMIsolation Issues
- Resource is Visible (Not Locked) During
Transaction - Resource is Visible if You Crash During
Transaction, Before Recovery is Done - Client Does Not Know When Transaction has
Completed - Nothing Must Depend on Results of the Transaction
Until It has Completed - Application Must Handle Isolation Issues
26CRMDeployment
- CRMs are COM Components
- CRMs Can Be Deployed
- In Server applications
- In Library applications
- CRMs Can Only Run in Server Applications
- Must Enable Compensating Resource Managers
- In Component Services Explorer - (server)
application Properties Advanced
27CRMCOM Configuration
- CRM Worker
- Requires transaction (or new transaction)
- Sync yes, JIT yes (because it has a
transaction) - Not main threaded (Both recommended)
- CRM Compensator
- No transaction
- Not main threaded (Both recommended)
- Some limits on Apartment threaded components
28CRMRecovery
- Marking an Application as CRM-Aware Means
Recovery Process Occurs at Application Startup - CRM Recovery Infrastructure Creates CRM Clerk
Instances to Assist in Recovery - As Records are Read from the Log, CRM Clerks
Create CRM Compensators as Needed
29CRMWhat a CRM Wont Do
- The CRM Infrastructure Provides No Concurrency
Management - The CRM Infrastructure Provides No Internal
Atomicity or Consistency
30Agenda
- COM Context
- Advanced COM Services Concepts
- Compensating Resource Manager (CRM)
- Object Pooling
- Events
- Security
31Object PoolingRecap Resource Dispensers
- MTS Introduced the Concept of Resource Dispensers
- A service that provides the synchronization and
management of non-durable resources within a
process - Resource Dispensers Provided For
- ODBC data access (ODBC 3.0 and above driver
manager) - OLEDB data access (OLEDB 2.0 and above service
components) - Mainframe connections - COMTI
32Object PoolingRecap Resource Dispensers
- Resource Dispensers are Convenient to Use
- Keep the same programming model
- Transaction enlistment is automatic
- Connection pooling is automatic
- Resources are garbage-collected via a timeout
value
33Object PoolingRecap Resource Dispensers
- Size of Connection Pool is Not Configurable
- Connection Pools Do Not Wait if Resource is Not
Available - You Can Write Your Own Resource Dispenser
- But its not very easy
34Object PoolingWhat are Pooled Objects?
- Simply Lightweight Resource Dispensers
- Only Appropriate for Last Tier Data Object Access
- Pooled Objects are Declaratively Configurable
- Maximum number in the pool
- Minimum number in the pool
- Time to wait for resource to become available
35Object PoolingPooled Objects
- When Do You Use a Pooled Resource Object?
- If objects are homogenous
- If long initialization time
- If precious resource
- If Time To Construct is Long Compared to Time Used
36Object PoolingPooled Object Requirements
- Must Have No Thread Affinity
- Must Manage Own State
- Must Implement IObjectControl
- Must Be Configured for Object Pooling
- Cannot Be Created with Visual Basic
- Visual Basic 6.0 components still have thread
affinity
Demo
37Object PoolingTransaction Management
- COM Does Transaction Management For You
- Disable Automatic Connection Pooling
- Disable Automatic Transaction Enlistment in OLEDB
38Object PoolingLifetime Management
- Implement IObjectControl
- Activate
- Reset to initial state
- Deactivate
- Return to the pool
- CanBePooled
- Decide whether it can be reused
39Object Pooling Error Handling
- What If the DBMS is Unavailable?
- ODBC 3.5 has configurable retry wait time
- In your object, handle this yourself
- Retry connection
- Return immediate bad return code
- Answer NO to CanBePooled to destroy object
40Agenda
- COM Context
- Advanced COM Services Concepts
- Compensating Resource Manager (CRM)
- Object Pooling
- Events
- Security
41Events
- Event Filtering
- Parallel Firing
- Combining With Other COM Services
- Queued Components
- Programmatic Configuration
42EventsFiltering
- Publisher Filtering
- Controls the order and firing of an event method
by an EventClass - Allows you to determine which subscribers will
receive a particular event - Parameter Filtering
- Used on a per-method, per-subscription basis and
provides a level of subscriber filtering at the
event source - Occurs after publisher filtering
43EventsFiltering
Publisher
Subscribers
MSFT
Event class
CPQ
INTC
interface IStockEvents HRESULT
StockPriceChange ( BSTR StockSymbol, double Price
)
- Subscribers Called When a Specific Stock Price
Changes - This Requires Filtering
44EventsFiltering
- Publisher Filtering
- Declarative
- Attribute of the EventClass
- Gets created by the COM EventSystem
- Filtering to be taken care by the Filter
component - Runtime
- Created by the user and set on the EventClass
programmatically - Filtering to be taken care by the Filter
component - Parameter Filtering
- Filter Criteria is set on the Subscription
- Filtering is taken care by the COM EventSystem
45EventsParallel Firing
- Set as an attribute on EventClass
- Multiple subscribers can receive events in
parallel - Publisher will still be blocked until the last
subscriber completes its processing
46EventsCombining with Queued Components
- COM Events Include Full Support for Queued
Components - Event Classes and/or Subscriberscan be queued
- Prevents event publisher from being blocked by a
subscriber - Enhances availability
- Ensures events reach remote subscribers over
unavailable or unreliable networks
47EventsQueue-able Subscriber
Publisher
48EventsProgrammatic Configuration
- Use COM Catalog
- Register event classes
- Manage subscriptions
Demo
49Agenda
- COM Context
- Advanced COM Services Concepts
- Compensating Resource Manager (CRM)
- Object Pooling
- Events
- Security
50Security
- COM Security Architecture
- Using COM Security
- Authentication
- Authorization (Access Control)
- Two-tier and Three-tier scenarios
- COM with SQL Server 7.0
51COM Security Architecture What is
Authentication?
- Verifying the Authenticity of a Claim of Identity
- For COM Applications, Authentication
- Can be turned on
- Is configured administratively
- Works Transparently to the Application
52COM Security Architecture Authorization
Roles
Client
Method Call
COM Executive
53COM AuthorizationOverview
Roles
Client
Method Call
COM Executive
54COM AuthorizationRoles
?
55COM AuthorizationSecurity Call Context
- Security Information Across Nested Calls
- Exposed as a property collection
- Caller identity (all upstream callers)
- Authentication service and level etc.
- Good Foundation for Auditing
- Caveat As Trustworthy as Direct Caller
Demo
56COM AuthorizationImpersonation / Delegation
- Requires Kerberos (Auto Selected by COM, If
Possible) - Main Drawback Performance
- Two authentications per call
- Expensive (late) access failure
- Defeats connection pooling
57COM AuthorizationThree-Tier Scenarios
58COM and SQL Server 7.0Three-Tier Scenario
- Map COM Roles to SQL Server
- Richer access control in database
- Suitable for most access control policy types
- Uses Application Roles in SQL Server 7.0
- Role is a database specific identity
SQL Server 7.0
59COM and SQL Server 7.0Three-Tier Scenario
- Restrict Access to Database to Trusted Components
/ Applications - Allows use of null password on application roles
- Avoid SQL Server application role password
management - Caveat
- Database audit is performed under the server
identity
60Call to Action
- Simplify Your Applications
- COM Context for declarative configuration and
execution - Get out of the security programming business with
COM Security - Enhance Your Applications with Advanced COM
Services - Compensating Resource Managers (CRM)
- Object Pooling
- Events
61MSDNEssential Resources For Developers
62Where can I get MSDN?
- Visit MSDN Online at msdn.microsoft.com
- Register for the MSDN Flash email newsletter at
msdn.microsoft.com/resources/msdnflash.asp - Join the MSDN Online Membership at
msdn.microsoft.com/community - Become an MSDN CD Subscriber at
msdn.microsoft.com/subscriptions - Attend more MSDN events!
63Questions?
64More Resources
- msdn.microsoft.com
- msdn.microsoft.com/osig/win2000
- www.microsoft.com/windows2000/ready
- msdn.microsoft.com/vstudio/win2000
- msdn.microsoft.com/winlogo/win2000.asp
- www.microsoft.com/dna
- www.microsoft.com/data/oledb/olap
65Additional Resources from Microsoft
- Inside COM Base Services by Guy and Henry Eddon
- Understanding COM by David Platt
Pick up your CD today with content from these and
other Windows 2000 programming titles.
Microsoft Press Resources available through
resellers and on the web at http//mspress.microso
ft.com/developer
66(No Transcript)