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í :

Č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.

Creating a New COM+ Application

Creating a new COM+ application requires two basic steps, as follows:

To create an empty COM+ application

  1. In the console tree of the Component Services administrative tool, select the computer on which you want to create an application.
  2. Select the COM+ Applications folder for that computer.
  3. 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.
  4. 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.
  5. 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.

  6. 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.

  7. 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.
  8. 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.
  9. 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

  1. In the console tree of the Component Services administrative tool, select the computer hosting the COM+ application.
  2. Open the COM+ Applications folder for that computer, and select the application in which you want to install the component(s).
  3. Open the application folder and select Components.
  4. 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.
  5. 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) .
  6. In the Install new components dialog box, click Add to browse for the component you want to add.
  7. 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.

  8. 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:

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:

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:

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:

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.


Praktické príklady na technológiu COM+

Príklad č.1 : vytvorenie COM objektu

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.