Compartir a través de


Object Pooling Technology Sample

This sample demonstrates the use of Just-in-time (JIT) Activation and object pooling in a .NET Framework environment. A server application contains the single pooled object, which represents a single log file. Several client applications instantiate and call into the pooled object class to show that, with JIT Activation enabled and a maximum pool size of one object, all client calls are routed through a single object instance. This results in a single log file that documents COM+ activity and multiple client calls.

For information about using the samples, see the following topics:

To build the sample using the command prompt

  1. Open the Command Prompt window and navigate to one of the language-specific subdirectories under the Technologies\ComponentServices\ObjectPooling directory.

  2. Type msbuild ObjectPoolingCS.sln or msbuild ObjectPoolingVB.sln, depending on your choice of programming language, at the command line.

To build the sample using Visual Studio

  1. Open Windows Explorer and navigate to one of the language-specific subdirectories under the ObjectPooling directory.

  2. Double-click the icon for ObjectPoolingCS.sln or ObjectPoolingVB.sln, depending on your choice of programming language, to open the file in Visual Studio.

  3. On the Build menu, click Build Solution. The OPDemoSvr.dll and the OPDemo.exe assemblies are built in the default \bin or \bin\Debug directory.

To register and install the assembly in the GAC

  1. Navigate to the directory that contains the built assembly.

  2. Type regsvcs OPDemoSvr.dll at the command line to register the assembly.

  3. Type gacutil –i OPDemoSvr.dll at the command line to install the assembly in the GAC.

To run the sample

  1. Open the Command Prompt window or Windows Explorer and navigate to the directory that contains the new executable.

  2. In the Administrative Tools submenu of your All Programs menu, open the Component Services administrative tool.

  3. Locate the OPDemoSvr COM+ application by expanding Component Services\Computers\My Computer\COM+ Applications in the left COM+ explorer pane.

  4. Locate the Microsoft.Samples.Technologies.ComponentServices.ObjectPooling.PooledLogFile component by expanding OPDemoSvr\Components. Right-click the PooledLogFile component and select Properties from the popup menu. Click the Activation tab and note that object pooling is enabled.

    NoteNote

    The component is configured with a minimum pool size of zero and a maximum pool size of one. This means that, at most, a single pooled object will exist at any time, no matter how many objects are instantiated from client code. These attributes are set explicitly in the sample code using the ObjectPoolingAttribute. The 60-second creation timeout is a default value that is applied because the sample code does not explicitly request a different value.

  5. Note the contents of the Object Construction edit field. This field contains the name of the disk-based log file that will be created by the sample pooled object. Its default value is OPSvrLogCS.txt or OPSvrLogVB.txt. You can change the name or add path information to the file name (for example, C:\OPSvrLogCS.txt). If you do not add path information to the file name, the file is created in the current System32 directory.

  6. Click OK to close the OPDemoServer component properties dialog box.

  7. Start three copies of OPDemo.exe from Windows Explorer or in the Command Prompt window. For example, in the Command Prompt window, navigate to the C:\Documents and Settings\Your User Name\My Documents\Samples\Technologies\ComponentServices\ObjectPooling\VB\ObjectPooling\bin directory and type opdemo.exe three times.

  8. From each client application, click the Start Writing button. You will see a blinking asterisk on the client window, which indicates that entries are being written to the log file specified in the OPDemoServer component object construction string.

  9. After all client applications have begun writing, wait several seconds. Then, click the Stop Writing button in each client application.

  10. Locate and open the log file in the Notepad.exe application or other text editor. You should see a sequence of statements that indicate COM+ calls to IObjectControl. In the middle of these calls, you will find written messages from each client application (identified by its process ID), demonstrating that COM+ has coordinated calls through three object references against a single instantiated object. For more information about the IObjectControl interface, see the sample code comments and the Platform SDK.

    NoteNote

    To rename or delete the log file, you must exit all instances of the client application and close the OPDemoSvr application in the Component Services administrative tool. To do this, right-click the COM+ application in the left pane of the explorer and select Shut Down from the pop-up menu.

Requirements

Platform: Windows 2000, Windows XP, Windows Server 2003

.NET Framework Version: 2.0, 1.1, 1.0

Remarks

This sample uses an object construction string to specify the log file name and potentially its fully qualified path. Just-in-time (JIT) Activation is a COM+ service that permits clients to create and hold on to an object reference for an indefinite time without requiring that an actual object instance exist behind it. COM+ activates an object just in time for a consumer when it calls through its object reference. This activation might consist of creating a new object instance or, when used in combination with COM+ object pooling, passing the caller a reference to a pooled object. COM+ deactivates an object based on the doneness bit on the object context. For .NET serviced components, the AutoCompleteAttribute can be applied to class methods, indicating that the object can be deactivated on exit from the method.

The C# component, as viewed in the Component Services administrative tool, is named OPDemoServerCS.PooledLogFile. The Visual Basic version is OPDemoServerVB.PooledLogFile. For more information about sample binary creation and registration steps, review the comments in the source code files.

NoteNote

You must use the Global Assembly Cache Tool (Gacutil.exe) to install the OPDemoSvr.dll assembly into the global assembly cache before you run the sample. Gacutil.exe is located in the SDK\<version number>\Bin directory.

COM+ object pooling is a service that will maintain a pool of active objects, ready to be used by a client that requests a component configured for pooling. The pool can be configured either administratively or by using the ObjectPoolingAttribute. Pool attributes include a minimum and maximum size of the pool and an object creation request time-out value.

Components that are enabled for JIT Activation and object pooling can control how they are reused by implementing the IObjectControl interface. IObjectControl methods are called only by COM+ to indicate key events in the object activation/deactivation process or to determine whether the object can be returned to the object pool. This sample implements IObjectControl only to demonstrate the interface being used by COM+. For more information about JIT Activation and object pooling, see the Platform SDK documentation.

See Also

Reference

Global Assembly Cache Tool (Gacutil.exe)
CanBePooled
ObjectPoolingAttribute
JustInTimeActivationAttribute
ServicedComponent
System.EnterpriseServices

Concepts

Just-in-Time Activation
Object Construction

Other Resources

Writing Serviced Components