COM+ ( Component Services)
zhrnutie a niekoľko príkladov
Základné informácie
Účel
COM+ je ďalšia evolúcia COM modelu a Microsoft transaction
Servra (MTS). COM+ stavia a rozširuje aplikácie
napísané pomocou technológie COM, MTS, a
iných technológií založených na COM. COM+
sa stará o mnoho úloh spojených s manažmentom
zdrojov, ktoré museli predtým vývojári
programovať, ako napríklad alokácia a bezpečnosť
vlákien.
Miesto použitia
COM+ môže byť použitý na vývoj
podnikových, kritických, distribuovaných
aplikácií založených na platforme Microsoft
Windows.
Cielení vývojári
COM+ je dizajnovaný hlavne pre Microsoft Visual C++ a Microsoft
Visual Basic vývojárov. Môže byť tiež
využitý na nasadenie aplikácií.
Nároky na runtime
COM+ vo verzii 1.5 je dodávané s a vyžaduje rodinu
Microsoft Windows XP, alebo Windows Server 2003 + operačných
systémov. COM+ vo verzii 1.0 je dodávaný spolu s a
vyžaduje Microsoft Windows 2000. COM+ aplikácie môžu byť
spúšťané taktiež na MS Windows 98 a MS Windows NT
klientoch.
Typy COM+ aplikácií
Sú nasledovné štyri základné typy COM+
aplikácií :
- Server aplikácie - COM+ server aplikácia
beží vo vlastnom procese. Server aplikácie môžu
podporovať všetky COM+ services.
- Library aplikácie - COM+ library aplikácia
beží v procese klienta, ktorý ju vytvoril. Presnejšie,
komponenty v libraty aplikácii sú vždy natiahnuté
do procesu vytvárateľa. Library aplikáci niesú
explicitne zviazané s procesom servra. Môžu
používať role-based bezpečnosť, ale nepodpodporujú
vzdialený prístup alebo queued komponenty.
- Aplication proxies - Aplication proxy je množina súborov
obsahujúcich registračné informácie
dovoľujúce klientovi vzdialene pristupovať k server
aplikáciám. Keď sú spustené na klientskom
počítači, aplkačný proxy súbor zapíše
informáciu o COM+ servrovej aplikácii, zahrňujúc
CLSIDy, ProgIDy, RemoteServerName, marshalling informáciu na
klientov počítač. Servrová aplikácia už môže
byť potom pristupovaná vzdialene z klientského
počítača.
- COM+ predinštalované aplikácie - COM+ zahŕňa
niekoľko predinštalovaných aplikácií, ktoré
sa starajú o intrné funkcie. Predinštalované
aplikácie sú v zozname v COM+ Applications zväzku v
Component Services administratívneho panelu, ale nemôžu
byť zmenené , alebo zmazané.
Časti COM+ aplikácie
COM+ aplikácia sa skladá z jedného alebo
viacerých COM komponentov. Táto sekcia definuje termy
component, objekt, trieda, interface.
- COM komponent je binárna jednotka,
ktorávytvára COM objekty (a ktorá zahŕňa baliaci a
registračný kód)
- COM objekt je inštancia COM triedy.
- COM trieda je pomenovaná, konkrétna
implementácia jedného, alebo viacerých
interfaceov. COM trieda je identifikovaná pomocou CLSID (
niekedy aj pomocou ProgID).
- COM interface, ktorý je exponovaný COM triedou, je
skupina účelovo blízkych funkcií, ktoré
špecifikujú kontrakt. Tento zahŕňa meno, podpis interfaceu,
sémantiku interfaceu, a formát marshalling bufferu.
Interface je identifikovaný pomocou IID. Syntax interfaceu je
definovaná v IDL a/alebo v knižniciach typov. Interfaces COM
tridy môžu byť tiež rozdelené do manažovateľných,
kohezívnych množín metód.
- COM metóda je zvyčajne dedna, alebo niekoľko
bízkych funkcií a je pokytovaná cez COM interface.
Creating a New
COM+ Application
Creating a new COM+ application requires two basic steps, as
follows:
To create an empty COM+ application
- In the console tree of the Component Services administrative
tool, select the computer on which you want to create an application.
- Select the COM+ Applications folder for that computer.
- On the Action menu, point to New, and then click
Application. You can also right-click the COM+
Applications folder, point to New, and then click Application.
- On the Welcome page of the COM+ Application Install
Wizard, click Next, and then in the Install or Create a New
Application dialog box, click Create an empty application.
- In the box provided, type a name for the new application. (Note
that the following special characters cannot be used in an application
name: \, /, ~, !, @, #, %,
^, &, *, (, ), |, },
{, ], [, ', ", >, <,
., ?, :, and ;.) Under Activation
type, click Library application or Server application.
Click Next.
Note A server
application runs in its own process. Server applications can support
all COM+ services. A library application runs in the process of the
client that creates it. Library applications can use role-based
security but do not support remote access or queued components.
- In the Set Application Identity dialog box, choose an
identity under which the application will run. If you select This
user, type the user name and password. You must also retype the
password in the Confirm password box. Click Next. (The
default selection for application identity is Interactive User.
The interactive user is the user logged on to the server computer at
any given time. You can select a different user by selecting This
user and entering a specific Microsoft® Windows® 2000 user
or group.)
Note The Set
Application Identity dialog box appears only if you selected Server
application for the new application's activation type in the COM
Application Install Wizard's preceding dialog box. The identity
property is not used for library applications.
- In the Add Application Roles dialog box, add any roles
you want to associate with the application. By default, only the CreatorOwner
role is defined. For information on roles, see Role-Based Security Administration.
- In the Add Users to Roles dialog box, populate each role
you created in the last step with the users, groups, or built-in
security principals to which you want to grant the privileges
associated with that role. By default, the interactive user is placed
in the CreatorOwner role.
- Click Finish.
The new application will now be displayed under the COM+
Applications folder in the console tree of the Component Services
administrative tool.
Installing New
Components
To add components to the Components folder of a COM+
application, you can either use the COM+ Component Install Wizard or
drag .dll files that contain the components from the Windows Explorer
and drop them in the application.
If you use the COM+ Component Install Wizard, you can either install
a new component, which registers the component on the computer, or
import components that have already been registered. Components can be
added to empty applications or existing applications.
To add a component to a COM+ application
- In the console tree of the Component Services administrative
tool, select the computer hosting the COM+ application.
- Open the COM+ Applications folder for that computer, and
select the application in which you want to install the component(s).
- Open the application folder and select Components.
- On the Action menu, point to New, and then click
Component. You can also right-click the Components
folder, point to New, and then click Component.
- On the Welcome page of the COM+ Application Install
Wizard, click Next, and then in the Import or
Install a Component dialog box, click Install new component(s) .
- In the Install new components dialog box, click Add to
browse for the component you want to add.
- In the Select files to install dialog box, type the
filename of the component to install or select a filename from the
displayed list. Click Open.
After you add the files, the Install new
components dialog box displays the files you have added and their
associated components. If you select the Details check box, you
will see more information about file contents and the components that
were found.
Note Unconfigured COM
components must have a type library. If COM+ cannot find your
component's type library, your component will not appear in the list.
You can also remove a file from the Files to install list by
selecting it and clicking Remove.
- Click Next, and then click Finish to install the
component.
COM+ Services
The following topics in this section contain concept and task
information for all the COM+ services:
COM+ Application
Pooling Concepts
COM+ Application Pooling allows single-threaded processes to scale,
similar to the way that thread pooling allows single-threaded objects
to scale. Application Pooling can also help recover from failures in
single processes by providing other running processes able to handle
activations.
Note Library
applications have the recycling and pooling properties of their host
process.
All methods of the ICOMAdminCatalog2 interface support Application
Pooling.
COM+ Application Pooling is configurable per application by using
the ConcurrentApps property of the COMAdminCatalogObject object in the
Applications collection. ConcurrentApps is an integer value
that specifies the maximum number of simultaneous Dllhost processes
that should be started to service activations for an application. This
property can be set by using the Component Services administration tool
or programmatically by using the COM+ Administration SDK.
If the ConcurrentApps property is set to 1, which is the
default value, the COM+ Application Pooling service is disabled.
COM+ Application
Recycling Concepts
Application recycling can significantly increase the overall
stability of your COM+ applications by offering a quick fix for known
problems and helping to protect against unexpected ones. For example,
application performance can degrade over time because of problems such
as memory leaks, nonscalable resource usage, and process failure. COM+
provides application recycling as a solution to these problems. You can
use application recycling to automatically shut down a process and
restart it, thus reinitializing a failing process and reallocating
memory it uses.
Application recycling works by creating a duplicate of the Dllhost
process associated with an application. This duplicate Dllhost process
services all future object requests, which leaves the old Dllhost to
finish servicing the remaining object requests. The old Dllhost process
is shut down when it detects the release of all external references to
objects in the process or when the expiration time-out value is
reached. Through this behavior, application recycling ensures that a
client application does not experience a service interruption.
Note You cannot
recycle a COM+ application that has been configured to run as a Windows
service. Also, library applications have the recycling and pooling
properties of their host process.
COM+ Applications Running as NT
Services
COM+ developers can now use the Component Services administrative
tool to configure and implement a COM+ server application as an NT
service. This means that the server can be automatically started or
restarted if your application always needs to be running; that your
COM+ application can run as the local system account if it needs to
perform privileged operations; and that your application's dependent
services can now be started automatically.
The topics described in the following table provide background and
task information about configuring your applications to run as NT
services.
COM+
Just-in-Time Activation Concepts
The Just-in-Time (JIT) Activation service enables COM+ to deactivate
an object while a client still holds an active reference to that
object. The next time the client calls a method on the object, which
the client believes to be still active, the COM+ JIT Activation service
reactivates the object transparently to the client, just in time.
The main advantage of using COM + JIT Activation is that you can
enable clients to hold references to objects for as long as they need
them, without necessarily tying up valuable server resources such as
memory. Other important benefits include the following:
- Using the COM+ JIT Activation service greatly simplifies the
programming model for the client because the client doesn't have to
think about how it uses expensive server objects and server resources.
Without JIT Activation, clients can incur a significant penalty when
they frequently need to call and release objects.
Note You can refine
this performance benefit further by using the COM+ Object Pooling
service. By pooling JIT-activated objects, you can greatly speed object
reactivation for clients while reusing whatever resources they might be
holding, giving you more precise control over how much memory is used
by a given object on the server. For more detail, see Object Pooling
and COM+ JIT Activation.
- With distributed applications, an expensive network round-trip is
required for the creation of every object, and the farther the client
is from the server, the greater the costs of activating and marshaling
the server object, opening the channel, and setting up the proxy and
stub. By using the COM+ JIT Activation service, you can minimize the
frequency of object creation to significantly improve the performance
of your application.
- When you use COM+ JIT Activation to activate those objects to
which clients hold long-lived references but which they aren't
necessarily using all the time, server memory is not always tied up
keeping those objects alive. This can significantly increase the
scalability of your application. The only performance hit that clients
see is the time it takes COM+ to reactivate the object, usually just
marginally more time than it takes to allocate memory for the object
and substantially less than the network round-trip for remote object
creation.
Enabling COM+ JIT Activation
You can enable the COM+ JIT Activation service for a component by
using either the Component Services administrative tool or the
Administrative SDK functions. For details about how to do this, see
Enabling JIT Activation for a Component.
COM+ JIT Activation can interact with other COM+ services, such as
the following:
- When your component requires transactions, JIT Activation is
automatically enabled for it. For greater detail, see Transactions and
JIT Activation.
- When your component is enabled for JIT Activation,
synchronization is automatically set to required. This means that if
two clients simultaneously call a JIT-activated component and a method
call for one of them returns, causing the object to be deactivated, the
other is not left stranded.
How Deactivation Is Triggered
COM+ deactivates an object based on the status of the doneness bit
on the object context. Your object can use this bit to signal whether
or not it is done—that is, ready to be deactivated—during a given
method call. For more information, see Setting the Done Bit.
Using the Auto-Done Property
Using the Component Services administrative tool, you can configure
a method such that the object is automatically deactivated on method
return. (See Enabling Auto-Done for a Method for instructions about how
to set this property.) By selecting this option, you can eliminate the
repetitive method calls for voting in transactions. Because the default
setting for the consistency bit is True, if you have changed
the done bit to True as well and you take no action to change
these settings, IObjectContext::SetComplete is called automatically
after the method returns.
However, there is one caveat to this behavior: COM+ will examine the
HRESULT that the method returns. If that HRESULT indicates failure, the
consistency bit is set to False and the result is the same as
if you had called IObjectContext::SetAbort.
To summarize, if you select auto-done for a method and don't take
any action to set any bits, and if an HRESULT(hr) is returned, the
following applies:
- If SUCCEEDS(hr), it is as though you called SetComplete.
- If FAILED(hr), it is as though you called SetAbort.
Using IObjectControl to Manage Object Activation and Deactivation
You can implement the IObjectControl interface so that the COM+
runtime automatically manages deactivation and reactivation for your
objects. When an object implements this interface, COM+ calls
IObjectControl::Deactivate when it deactivates the object and
IObjectControl::Activate when it reactivates it. These methods enable
automatic context initialization on object activation and cleanup of
state on deactivation.
If you are pooling objects that use COM+ JIT Activation, it is
highly recommended that you implement IObjectControl. For more
detail, see Object Pooling and COM+ JIT Activation.
Statelessness and JIT Activation
Transactional objects are necessarily stateless because you cannot
share state across a transaction boundary. Therefore, you would use JIT
Activation only when your object holds no state that would be lost on
deactivation; otherwise, you violate the isolation of the transactions.
Due to the natural use patterns of transactional objects—they do some
unit of work and release the object when the transaction commits or
aborts—JIT activation and automatic transactions are closely related.
Configuring an object to require transactions enables COM+ JIT
Activation automatically.
COM+ Object Pooling
Concepts
Object pooling is an automatic service provided by COM+ that enables
you to configure a component to have instances of itself kept active in
a pool, ready to be used by any client that requests the component. You
can administratively configure and monitor the pool maintained for a
given component, specifying characteristics such as pool size and
creation request time-out values. When the application is running, COM+
manages the pool for you, handling the details of object activation and
reuse according to the criteria you have specified.
You can achieve very significant performance and scaling benefits by
reusing objects in this manner, particularly when they are written to
take full advantage of reuse. With object pooling, you gain the
following benefits:
- You can speed object use time for each client, factoring out
time-consuming initialization and resource acquisition from the actual
work that the object performs for clients.
- You can share the cost of acquiring expensive resources across
all clients.
- You can pre-allocate objects when the application starts, before
any client requests come in.
- You can govern resource use with administrative pool
management—for example, by setting an appropriate maximum pool level,
you can keep open only as many database connections as you have a
license for.
- You can administratively configure pooling to take best advantage
of available hardware resources—you can easily adjust the pool
configuration as available hardware resources change.
- You can speed reactivation time for objects that use Just-in-Time
(JIT) Activation, while deliberately controlling how resources are
dedicated to clients.
Writing Poolable Objects
Poolable objects must meet certain requirements to enable a single
object instance to be used by multiple clients. For example, they can't
hold client state or have any thread affinity. Transactional objects
also have particular requirements, in that managed resources held by a
pooled object must be manually enlisted in a transaction.
Pooled objects can implement IObjectControl to control how they are
reused. This enables them to perform initialization when activated in a
given context, to clean up any client state on deactivation, and to
indicate when they are in a non-reusable state.
Often, it useful to write poolable objects in a somewhat generic
fashion so that they can be administratively customized with a
constructor string. For example, an object might be written to hold a
generic ODBC connection, with a particular DSN administratively
specified in a constructor string.
The topics in this section, described in the following table,
provide information about how object pooling works in COM+, as well as
information about how to write, configure, and implement poolable
objects.
Tento príklad demonštruje použitie reťazcov
objekt konštruktora
v .NET prostredí. Reťazce konštruktora objektu sú
inicializačné reťazce, ktoré môžu byť
špecifikované a editované administratívne (cez
administračný nástroj -Component Services administrative
tool), vylučujúc tak potrebu natvrdo kódovať
konfiguračné parametre potrebné pre štart komponentu.
Inštrukcie , archív potrebných
súborov.
Príklad č.2 :Poolovanie objektu (jeho zdieľanie
viacerými klientmi) a just-in-time aktivácia objektu
Príklad demonštrujúci použitie Just In Time
aktvácie a Object Pooling v rostredí .NET.
Jednoduchý poolovaný objekt zapisuje reťzce do log
súboru. Viacero klientských aplikácií
vykonáva paralelne volania na triedu poolovaného objektu.
S JIT aktiváciou a maximálnym možným pool
parametrom na jeden objekt, všetky klientské volania sú
presmerované na jednu inštanciu objektu. Výsledkom je
jediný log súbor ktorý dokumentuje COM+ aktivitu o
obsluhe pool volaní a tiež ukacuje , že viacero pool klientov
volajú ten istý objekt. Reťazec object konštruktora (viď
predchádzajúci príklad) je použitý na
určenie log súboru s potenciálne celou cestou k nemu.
Inštrukcie , archív potrebných
súborov.
Príklad č.3 : queued components (funkcia COM+ services
na radenie správ do fronty)
Queued components je COM+ izéé založené na Message
Queueing Services (MSMQ, tudíž toto treba nainštalovať, ak
chcete vyskúšať príklad) , ktorý ponúka
mechanizmus na volanie a vykonávanie componentov
asynchrónne. Keď klient zavolá queued komponent, volanie
je automaticky zaregistrované nahrávačom, ktorý ho
zaobalí ako správu a nechá vo fronte správ.
Listner si prečíta správu z frony a podá ju ďalej
playeru. Player vykoná tie správne volania metód
na server objekte.
Inštrukcie, archív potrebných
súborov.
Príklad č.4 :COM+ Role-Based Security Sample
Tento príklad demonštruje niektoré aspekty COM+
Role-Based Secutiry v .NET prostredí. Ukážka pristupuje k
security-related metódam v ContextUtil triede aby zistila
run-time informáciu o bezpečnostnom prostredí.
Príklad tiež volá SecurityCallContext objekt aby zistil
account information aktuálneho volajúceho.
Informácie, ktoré sú získané : či je
COM+ bezpečnosť zapnutá, či je volajúci členom secutiry
role vytvorenej príkladom, account name volajúceho.
Inštrukcie , archív potrebných
súborov.
Príklad č.4 : COM+ tranzakcie
Príklad demonštruje aspekty COM+ Transactions v .NET
prostredí. Príklad vyhľadá hodnotu z
databázy, potom umožní používateľovi túto
hodnotu updateovať. Potom ako používateľ špecifikuje novú
hodnotu premennej a vyberie AutoCompletePost alebo Post, príklad
zapíše hodnotu do databázy. Nasledovne príklad
zistí, či je zapísaná hodnota
prípustná ( arbitrárne to musí byť
číslo medzi 0 a 10). Ak hodnota nieje dovolená,
príklad zruší tranzakciu, v opačnom prípade ju
potvrdí. Nakoniev príklad ukáže aká je
nová hodnota v databáze.
Inštrukcie , archív potrebných
súborov.